forked from chilek/lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lms-fping
executable file
·200 lines (166 loc) · 4.97 KB
/
lms-fping
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#!/usr/bin/perl -Tw
#
# LMS version 1.11-git
#
# Copyright (C) 2001-2013 LMS Developers
#
# Please, see the doc/AUTHORS for more information about authors!
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License Version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
# $Id$
use strict;
use DBI;
use Config::IniFiles;
use Getopt::Long;
use vars qw($configfile $quiet $help $version);
$ENV{'PATH'}='/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin';
my $_version = '1.11-git';
my %options = (
"--config-file|C=s" => \$configfile,
"--quiet|q" => \$quiet,
"--help|h" => \$help,
"--version|v" => \$version
);
Getopt::Long::config("no_ignore_case");
GetOptions(%options);
sub mask2prefix($)
{
my $mask = shift @_;
my @tmp = split('\.',$mask,4);
my $q = sprintf("%b%b%b%b",$tmp[0],$tmp[1],$tmp[2],$tmp[3]);
$q =~ s/0*$//;
if ($q =~ /0/) {
print " You idiot. error in mask\n";
}
my $len = length($q) ;
return $len;
}
if($help)
{
print STDERR <<EOF;
lms-fping, version $_version
(C) 2001-2013 LMS Developers
-C, --config-file=/etc/lms/lms.ini alternate config file (default: /etc/lms/lms.ini);
-h, --help print this help and exit;
-v, --version print version info and exit;
-q, --quiet suppress any output, except errors;
EOF
exit 0;
}
if($version)
{
print STDERR <<EOF;
lms-fping, version $_version
(C) 2001-2013 LMS Developers
EOF
exit 0;
}
if(!$configfile)
{
$configfile = "/etc/lms/lms.ini";
}
if(!$quiet)
{
print STDOUT "lms-fping, version $_version\n";
print STDOUT "(C) 2001-2013 LMS Developers\n";
print STDOUT "Using file $configfile as config.\n";
}
if(! -r $configfile)
{
print STDERR "Fatal error: Unable to read configuration file $configfile, exiting.\n";
exit 1;
}
my $ini = new Config::IniFiles -file => $configfile;
print @Config::IniFiles::errors;
my $networks_list = $ini->val('fping','networks') || '';
my $excluded_networks_list = $ini->val('fping','excluded_networks') || '';
my $fping = $ini->val('fping','fping_binary') || '/usr/sbin/fping';
my $tmpfile = $ini->val('fping','temp_file') || '/tmp/fping_hosts';
my $dbtype = $ini->val('database', 'type') || 'mysql';
my $dbhost = $ini->val('database', 'host') || 'localhost';
my $dbuser = $ini->val('database', 'user') || 'root';
my $dbpasswd = $ini->val('database', 'password') || '';
my $dbname = $ini->val('database', 'database') || 'lms';
my $dbase;
my $utsfmt;
if($dbtype =~ /mysql/)
{
$dbase = DBI->connect("DBI:mysql:database=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
$utsfmt = "UNIX_TIMESTAMP()";
}
elsif($dbtype eq "postgres")
{
$dbase = DBI->connect("DBI:Pg:dbname=$dbname;host=$dbhost","$dbuser","$dbpasswd", { RaiseError => 1 });
$utsfmt = "EXTRACT(EPOCH FROM CURRENT_TIMESTAMP(0))";
}
else
{
print STDERR "Fatal error: unsupported database type: $dbtype, exiting.\n";
exit 1;
}
if(!$networks_list)
{
my $dbq = $dbase->prepare("SELECT name FROM networks");
$dbq->execute();
while (my $row = $dbq->fetchrow_hashref()) {
$networks_list = "$networks_list $row->{'name'}";
}
$dbq->finish();
}
my @networks = split ' ', $networks_list;
if($excluded_networks_list)
{
my @excluded_networks = split ' ', $excluded_networks_list;
foreach my $excluded_network (@excluded_networks)
{
@networks = grep(!/^$excluded_network$/, @networks);
}
}
# create temporary file with list of hosts
open(TMPFILE, ">$tmpfile") or die("Fatal error: Unable to write $tmpfile, exiting.\n");
foreach my $network (@networks)
{
my $dbq = $dbase->prepare("SELECT name, INET_NTOA(address) AS address, mask FROM networks WHERE UPPER(name) = UPPER('$network')");
$dbq->execute();
if(my $row = $dbq->fetchrow_hashref())
{
my $netsize = 2**(32 - mask2prefix($row->{'mask'}));
my $ndbq = $dbase->prepare("SELECT INET_NTOA(ipaddr) AS ipaddr FROM vnodes WHERE ipaddr>INET_ATON('$row->{'address'}') AND ipaddr<INET_ATON('$row->{'address'}')+$netsize");
$ndbq->execute();
while(my $nodrow = $ndbq->fetchrow_hashref())
{
print TMPFILE "$nodrow->{'ipaddr'}\n";
}
}
$dbq->finish();
}
close(TMPFILE);
#get list of on-line hosts (option -a)
my @lines = `$fping -ar1 -f $tmpfile 2>/dev/null`;
foreach my $line (@lines)
{
$line =~ s/^\s+//;
$line =~ s/\s+$//;
my $idbq = $dbase->prepare("UPDATE nodes SET lastonline=$utsfmt WHERE ipaddr=INET_ATON('$line')");
$idbq->execute();
if(!$quiet)
{
print "$line\n";
}
$idbq->finish();
}
$dbase->disconnect();
system("rm $tmpfile");