forked from open-ch/pingmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpingmachine
More file actions
executable file
·86 lines (72 loc) · 2.25 KB
/
pingmachine
File metadata and controls
executable file
·86 lines (72 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
###############################################################################
#
# Pingmachine - Smokeping-like Latency Measurement
#
# Written by David Schweikert <dws@open.ch>, June 2011
# Copyright (c) 2011-2014 Open Systems AG, Switzerland
# All Rights Reserved.
#
# See LICENSE file for the software licensing conditions.
#
###############################################################################
use strict;
use warnings;
use EV;
use Pingmachine::Main;
use Net::Server::Daemonize qw(daemonize);
use Getopt::Long;
use Log::Any qw($log);
use Log::Any::Adapter;
my %opt = ();
sub usage {
die "usage: pingmachine [--debug] [--user=USER] [--basedir=DIR] [--telegraf=HOST:PORT]\n".
" (default basedir: ".Pingmachine::Config->base_dir.")\n";
}
sub my_daemonize {
my ($uid, $gid, $groups);
if($opt{user}) {
($uid, $gid) = (getpwnam($opt{user}))[2,3];
defined $uid or die "ERROR: can't resolve username $opt{user}";
$groups = `id -G $opt{user}`; chomp $groups;
}
chdir '/tmp';
daemonize('root', 'root', undef);
# Net::Server::Daemonize support for supplementary groups is buggy -> we
# need to do it ourselves with $)
#
# Note that this is important because fping is executable only by
# pingmachine's secondary group monitor
if($opt{user}) {
$) = "$gid $groups";
$> = $uid;
}
EV::default_loop->loop_fork();
}
sub main {
# Parse arguments
GetOptions (\%opt, 'debug|d', 'help|h', 'user|u=s', 'basedir|b=s', 'telegraf|t=s');
usage() if $opt{help};
Pingmachine::Config->base_dir($opt{basedir}) if $opt{basedir};
Pingmachine::Config->set_telegraf($opt{telegraf}) if $opt{telegraf};
# Configure logging
if($opt{debug}) {
Log::Any::Adapter->set('Dispatch', outputs => [
[ 'Screen', min_level => 'debug', newline => 1 ],
]);
}
else {
Log::Any::Adapter->set('Dispatch', outputs => [
[ 'Syslog', min_level => 'info', ident => 'pingmachine' ],
]);
}
$EV::DIED = sub {
$log->error("INTERNAL ERROR: $@");
};
# Daemonize
my_daemonize() unless $opt{debug};
# Start Pingmachine
my $pingmachine = Pingmachine::Main->new();
$pingmachine->run();
}
main;