-
Notifications
You must be signed in to change notification settings - Fork 0
/
lms_asnmp.pl
executable file
·69 lines (53 loc) · 1.22 KB
/
lms_asnmp.pl
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
#!/usr/bin/perl
my $pidf = "/var/run/lms_asnmp.pid";
my $snmped_ip = "192.168.2.5";
my $f;
my $sqlf = "/var/run/lms_asnmp.sql";
my $sqf;
my $inoctets = -1;
my $outoctets = -1;
my $Iface = 6;
if (-f $pidf) {
open $f, "< $pidf"
or die "could not open pid file.";
my $pid_test = <$f>;
if (kill 0, "$pid_test") {
die "another lms_ping is running.";
}
}
open $f, "> $pidf"
or die "could not open pid file.";
print $f $$;
close $f;
my @snmp_output = `snmpwalk -v2c -cpublic $snmped_ip IF-MIB::ifOutOctets.$Iface`;
foreach my $line(@snmp_output) {
chomp $line;
if ($line =~ m/^IF-MIB::ifOutOctets.\d+ = Counter32: (\d+)$/) {
$outoctets = $1;
}
}
@snmp_output = `snmpwalk -v2c -cpublic $snmped_ip IF-MIB::ifInOctets.$Iface`;
foreach my $line(@snmp_output) {
chomp $line;
if ($line =~ m/^IF-MIB::ifInOctets.\d+ = Counter32: (\d+)$/) {
$inoctets = $1;
}
}
my $sqf;
open $sqf, "> $sqlf"
or die "could not open sql file";
print $sqf "INSERT INTO atable(inB, outB) VALUES (";
if ($inoctets == -1) {
print $sqf "NULL,";
} else {
print $sqf "\"$inoctets\",";
}
if ($outoctets == -1) {
print $sqf "NULL,";
} else {
print $sqf "\"$outoctets\"";
}
print $sqf ");";
`mysql -prootpass cs183 < $sqlf`;
unlink $sqlf;
unlink $pidf;