-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpollDevices.pl
116 lines (104 loc) · 4.53 KB
/
pollDevices.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
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
#!/usr/bin/perl
#Scipt to pull all devices as part of monitoring system
use Time::Piece;
use strict;
use warnings;
use DBI;
my $hostname = `hostname`; chomp $hostname;
my $ping = "ping -c1 -t2"; #Ping command
my $dbUser = "root"; #Database User
my $dbPass = ''; #Database password
my @devices;
##### Start DB Connection
my $dbh = DBI->connect('dbi:mysql:monitoring',$dbUser,$dbPass)
or die "Connection Error: $DBI::errstr\n";
#Debug DB connect
print "\nDEBUG: Connection Established to DB\n\n";
my $date = localtime->strftime('%F %T');
my $sql = "INSERT IGNORE INTO log VALUES ('','PollStart','','','$hostname','$date')";
print "DEBUG: SQL update: $sql\n";
my $sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
$sql = "SELECT name FROM objects WHERE monitored !='0' AND is_remote!=1;";
$sth = $dbh->prepare($sql);
$sth->execute
or die "Connection Error: $DBI::errstr\n";
while (my $device = $sth->fetchrow_array() ) {
print $device . "\n";
push @devices, $device;
}
foreach (@devices) {
my $device = $_;
$sql = "SELECT address FROM objects WHERE name='$_'";
my $sth = $dbh->prepare($sql);
$sth->execute
or die "Connection Error: $DBI::errstr\n";
my $address = $sth->fetchrow_array();
my $ping_out = `$ping $address 2> /dev/null`;
chomp ($ping_out);
if ($ping_out !~ /bytes from/) {
print "$hostname isn't pinging, logging\n";
$date = localtime->strftime('%F %T');
$sql = "INSERT IGNORE INTO log VALUES ('','$device','down','','$device','$date')";
print "DEBUG: SQL update: $sql\n";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
print "Updating to down status\n";
$sql = "UPDATE status SET last_down=NOW(),current_status=0 WHERE name='$device';";
print "DEBUG: SQL update: $sql\n";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
#####Need to add logic for last change into table here
print "Checking last status to confirm any changes...\n";
$sql = "SELECT current_status FROM status WHERE name='$device';";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
my $temp_status = $sth->fetchrow_array();
if ($temp_status == 0) {
print "Device was previously down, nothing to change or update\n";
}
else {
print "Device was previously up, modifying status accordingly...\n";
$sql = "UPDATE status SET last_change=NOW(),current_status=0 WHERE name='$device';";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
}
} else {
print "$device is up\n";
$sql = "UPDATE status SET last_up=NOW() WHERE name='$device';";
print "$sql\n";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
#####Need to add logic for last change into table here
print "Checking last status to confirm any changes...\n";
$sql = "SELECT current_status FROM status WHERE name='$device';";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
my $temp_status = $sth->fetchrow_array();
if ($temp_status == 1) {
print "Device was previously up, nothing to change or update\n";
}
else {
print "Device was previously down, modifying status accordingly...\n";
$sql = "UPDATE status SET last_change=NOW(),current_status=1 WHERE name='$device';";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
}
}
}
#####Log polling and script completion
$date = localtime->strftime('%F %T');
$sql = "INSERT IGNORE INTO log VALUES ('','PollComplete','','','$hostname','$date')";
print "DEBUG: SQL update: $sql\n";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr\n";
print "Done! $date\n";