| 1 |
#!/usr/bin/perl -w |
| 2 |
# vim:et:ts=2:sw=2: |
| 3 |
|
| 4 |
#********************************************************************* |
| 5 |
# |
| 6 |
# fai-monitor -- monitor daemon which collects client status info |
| 7 |
# |
| 8 |
# This script is part of FAI (Fully Automatic Installation) |
| 9 |
# (c) 2003-2015 by Thomas Lange, [email protected] |
| 10 |
# Universitaet zu Koeln |
| 11 |
# |
| 12 |
#********************************************************************* |
| 13 |
|
| 14 |
use strict; |
| 15 |
use Socket; |
| 16 |
use Getopt::Std; |
| 17 |
use POSIX qw(strftime); |
| 18 |
|
| 19 |
$| = 1; |
| 20 |
my ($port, $timeout, $daemon, $timestamp, $timestamp_format, $localtime); |
| 21 |
my $pidfile = '/var/run/fai-monitor.pid'; |
| 22 |
my $logfile = '-'; |
| 23 |
my $daemonlogfile = '/var/log/fai-monitor.log'; |
| 24 |
my $useip; |
| 25 |
|
| 26 |
our %variables; |
| 27 |
|
| 28 |
our ($opt_b,$opt_h,$opt_p,$opt_l,$opt_t,$opt_d,$opt_P,$opt_T,$opt_i,$opt_Z,$opt_f); |
| 29 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 30 |
sub logline(@) { |
| 31 |
open(LOGFILE, $logfile) or return 0; |
| 32 |
|
| 33 |
if ($timestamp) { |
| 34 |
if ($localtime) { |
| 35 |
print LOGFILE (strftime "$timestamp_format - ", gmtime()); |
| 36 |
} else { |
| 37 |
print LOGFILE (strftime "$timestamp_format - ", localtime()); |
| 38 |
} |
| 39 |
} |
| 40 |
print LOGFILE @_ or return 0; |
| 41 |
close(LOGFILE); |
| 42 |
return 1; |
| 43 |
} |
| 44 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 45 |
sub signal_die(@) { |
| 46 |
logline(@_); |
| 47 |
unlink($pidfile); |
| 48 |
exit(1); |
| 49 |
} |
| 50 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 51 |
sub signal_warn(@) { |
| 52 |
logline(@_) or die "log: $!"; |
| 53 |
} |
| 54 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 55 |
sub signal_deadly(@) { |
| 56 |
# Use the die-handler |
| 57 |
signal_die('Caught deadly signal ' . shift() . "\n"); |
| 58 |
} |
| 59 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 60 |
sub server_init() { |
| 61 |
logline("FAI monitoring daemon starting..\n") or die "log: $!"; |
| 62 |
|
| 63 |
# Init signals |
| 64 |
$SIG{INT} = \&signal_deadly; |
| 65 |
$SIG{QUIT} = \&signal_deadly; |
| 66 |
$SIG{TERM} = \&signal_deadly; |
| 67 |
$SIG{__DIE__} = \&signal_die; |
| 68 |
$SIG{__WARN__} = \&signal_warn; |
| 69 |
# HUP is usually used to reopen log files. This is not a problem |
| 70 |
# in this design. |
| 71 |
$SIG{HUP} = 'IGNORE'; |
| 72 |
|
| 73 |
if ($daemon) { |
| 74 |
if (-e $pidfile) { |
| 75 |
# Pid file already exists. Check if it's a valid pid. |
| 76 |
open(PIDFILE, '<', "$pidfile") or die "open $pidfile: $!"; |
| 77 |
my $pid = <PIDFILE>; |
| 78 |
chomp($pid); |
| 79 |
if ($pid ne '') { |
| 80 |
# Kill -0 exits with value 0 if pid is alive |
| 81 |
system("kill -0 $pid 2> /dev/null"); |
| 82 |
if ($? == 0) { |
| 83 |
logline("Pidfile $pidfile exists and contains an existing pid. Exiting.\n"); |
| 84 |
exit(1); |
| 85 |
} |
| 86 |
} |
| 87 |
close(PIDFILE); |
| 88 |
} |
| 89 |
eval "Proc::Daemon::Init"; |
| 90 |
umask 022; |
| 91 |
|
| 92 |
open(PIDFILE, '>', "$pidfile") or die "open $pidfile: $!"; |
| 93 |
print PIDFILE $$ or die "print $pidfile: $!"; |
| 94 |
close(PIDFILE); |
| 95 |
} |
| 96 |
|
| 97 |
# Listen |
| 98 |
my $proto = getprotobyname('tcp'); |
| 99 |
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; |
| 100 |
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsockopt: $!"; |
| 101 |
|
| 102 |
my $paddr = sockaddr_in($port, INADDR_ANY); |
| 103 |
|
| 104 |
bind(SERVER, $paddr) or die "bind: $!"; |
| 105 |
listen(SERVER, SOMAXCONN) or die "listen: $!"; |
| 106 |
logline("FAI monitoring daemon started on port $port with pid $$\n") or die "log: $!"; |
| 107 |
} |
| 108 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 109 |
sub big_loop() { |
| 110 |
|
| 111 |
# accept a connection, print message received and close |
| 112 |
my ($client_addr); |
| 113 |
while ($client_addr = accept(CLIENT, SERVER)) { |
| 114 |
my ($port, $iaddr) = sockaddr_in($client_addr); |
| 115 |
my $ip = inet_ntoa($iaddr); |
| 116 |
|
| 117 |
my $inp = ''; |
| 118 |
|
| 119 |
eval { |
| 120 |
local $SIG{__DIE__}; |
| 121 |
local $SIG{__WARN__}; |
| 122 |
local $SIG{'ALRM'} = sub { die("Timeout"); }; |
| 123 |
|
| 124 |
alarm($timeout); |
| 125 |
$inp = <CLIENT>; |
| 126 |
alarm(0); |
| 127 |
defined($inp) && $inp =~ /^VARIABLE / && reply_to_client($inp) |
| 128 |
}; |
| 129 |
|
| 130 |
close CLIENT; |
| 131 |
|
| 132 |
if (!defined($inp) || $inp eq '') { |
| 133 |
# Client did not send anything, or alarm went off |
| 134 |
logline("$ip:$port: No data or timeout.\n") or die "log: $!"; |
| 135 |
next; |
| 136 |
} |
| 137 |
|
| 138 |
if ($inp =~ /^([^\s;]+)\s+TASKEND install 0/ && $opt_b) { |
| 139 |
my $cname = $1; |
| 140 |
if ($useip) { |
| 141 |
$cname = $ip; |
| 142 |
} |
| 143 |
system('fai-chboot', '-d', $cname); |
| 144 |
logline("$ip:$port: Disabling pxelinux configuration for $cname\n") or die "log: $!"; |
| 145 |
} |
| 146 |
logline("$inp") or die "log: $!"; |
| 147 |
} |
| 148 |
} |
| 149 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 150 |
sub read_list_of_variables { |
| 151 |
|
| 152 |
# read a file which contains a list of variables |
| 153 |
# format: key=value |
| 154 |
# value may contain blanks |
| 155 |
# generate a hash |
| 156 |
|
| 157 |
my $varfile = "/var/log/fai/variables"; |
| 158 |
return unless -f $varfile; |
| 159 |
|
| 160 |
open(VARFILE, '<', "$varfile") or die "open $varfile: $!"; |
| 161 |
while (<VARFILE>) { |
| 162 |
next if /^#/; |
| 163 |
if (/^(\w+)=(.*)/) { |
| 164 |
$variables{$1} = $2; |
| 165 |
} |
| 166 |
} |
| 167 |
close(VARFILE); |
| 168 |
} |
| 169 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 170 |
sub reply_to_client { |
| 171 |
|
| 172 |
my $name = shift; |
| 173 |
|
| 174 |
chomp $name; |
| 175 |
$name =~ s/^VARIABLE //; |
| 176 |
|
| 177 |
unless (exists $variables{$name}) { |
| 178 |
print CLIENT "UNKNOWN $name\n"; |
| 179 |
return; |
| 180 |
} |
| 181 |
print CLIENT "OK $variables{$name}\n"; |
| 182 |
return; |
| 183 |
} |
| 184 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 185 |
sub usage { |
| 186 |
|
| 187 |
print << "EOF"; |
| 188 |
fai-monitor, FAI monitor daemon. |
| 189 |
|
| 190 |
Copyright (C) 2003-2015 by Thomas Lange |
| 191 |
|
| 192 |
Usage: fai-monitor [OPTIONS] |
| 193 |
|
| 194 |
-b Call fai-chboot to change boot parameter. |
| 195 |
-p PORT Set port to listen to. Default is 4711. |
| 196 |
-l FILE Logfile. Default is standard out and |
| 197 |
'$daemonlogfile' in daemon mode. |
| 198 |
-t TIMEOUT Timeout for bad clients. 0 to disable. |
| 199 |
-d Daemon mode. |
| 200 |
-P FILE PID-file. Default is '$pidfile'. |
| 201 |
Used only if starting in daemon mode. |
| 202 |
-T Print timestamps in the log. |
| 203 |
-f FORMAT Use strftime(FORMAT) for timestamp formatting. |
| 204 |
-Z Use gmtime/UTC instead of local time. |
| 205 |
-i When using -b: send IP of client to fai-choot |
| 206 |
instead of the hostname the host reports. |
| 207 |
|
| 208 |
EOF |
| 209 |
exit 0; |
| 210 |
} |
| 211 |
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
| 212 |
|
| 213 |
getopts('bhZTf:p:l:t:dP:i') || usage; |
| 214 |
$opt_h && usage; |
| 215 |
$port = $opt_p || 4711; |
| 216 |
$timeout = $opt_t || 5; |
| 217 |
$localtime = $opt_Z || 0; |
| 218 |
$daemon = $opt_d || 0; |
| 219 |
$timestamp = $opt_T || 0; |
| 220 |
$useip = $opt_i || 0; |
| 221 |
|
| 222 |
if (defined($opt_P)) { |
| 223 |
$pidfile = $opt_P; |
| 224 |
} |
| 225 |
|
| 226 |
if (defined($opt_f)) { |
| 227 |
$timestamp_format = $opt_f |
| 228 |
} else { |
| 229 |
$timestamp_format = "%a %b %e %H:%M:%S %Y"; |
| 230 |
} |
| 231 |
|
| 232 |
if (defined($opt_d)) { |
| 233 |
(eval "require Proc::Daemon") or |
| 234 |
die "Daemon mode not available, Proc::Daemon not found. Please install libproc-daemon-perl\n"; |
| 235 |
# If in daemon mode, use standard daemon log file |
| 236 |
$logfile = $daemonlogfile; |
| 237 |
} |
| 238 |
|
| 239 |
if (defined($opt_l)) { |
| 240 |
$logfile = $opt_l; |
| 241 |
} |
| 242 |
|
| 243 |
# Constuct a $logfile that open can take as an argument |
| 244 |
if ($logfile eq '-') { |
| 245 |
$logfile = ">&STDOUT"; |
| 246 |
} |
| 247 |
else { |
| 248 |
$logfile = ">>$logfile"; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
read_list_of_variables; |
| 253 |
server_init; |
| 254 |
big_loop; |