forked from lozzd/FITB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller_child.php
149 lines (120 loc) · 6.65 KB
/
poller_child.php
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
<?php
include_once('functions.php');
if(!defined('STDIN') ) {
echo "Must only be run from the command line";
die();
}
if(!isset($argv[1])) {
echo "This tool is only supposed to be run by the poller master. \n";
echo "If you must run it by hand it requires the host from the config to poll\n";
die();
}
$pollhost = $argv[1];
if(!isset($pollhosts[$pollhost]["prettyname"])) {
echo "Something went really wrong: Poll host requested not found in config!\n";
die();
}
# Fetch the details required for polling from the config
$pollprettyhost = $pollhosts[$pollhost]["prettyname"];
$pollip = $pollhosts[$pollhost]["ip"];
$pollsnmpcomm = $pollhosts[$pollhost]["snmpcommunity"];
$pollgraphs = $pollhosts[$pollhost]["graphtypes"];
# Connect carbon.
if (isset($carbon_host,$carbon_port,$graphite_prefix,$graphite_metrics,$graphite_datacenter)) {
$carbon = fsockopen($carbon_host, $carbon_port, $errno, $errstr, 5);
if (!$carbon) {
logline("{$pollprettyhost} - Connect to carbon failed.", 0, $verbose);
}
}
logline("{$pollprettyhost} - Starting poller run for this host. ", 0, $verbose);
$timestamp = time();
logline("{$pollprettyhost} - Beginning SNMP poll", 1, $verbose);
$ifEntry = snmptable($pollip, $pollsnmpcomm, "1.3.6.1.2.1.2.2.1");
$ifXEntry = snmptable($pollip, $pollsnmpcomm, "1.3.6.1.2.1.31.1.1.1");
logline("{$pollprettyhost} - SNMP poll complete", 1, $verbose);
# Here comes the fun. For every interface, we need to create every graph type that we decided we would graph in the config
if (!$ifEntry) {
logline("{$pollprettyhost} - SNMP Failed! Either no results, no response or timeout. ", 0, $verbose);
exit();
}
foreach($ifEntry as $intid => $thisint) {
logline("{$pollprettyhost} - Starting interface loop for interface index {$intid} ({$thisint[2]})", 1, $verbose);
# Check if the interface is up. No point graphing down interfaces.
if (($thisint[7] == "1") && ($thisint['8'] == "1")) {
# Assign the values to an array with names for easier referencing
$thisint['inoctets'] = $ifXEntry[$intid][6];
$thisint['outoctets'] = $ifXEntry[$intid][10];
$thisint['inucastpkts'] = $ifXEntry[$intid][7];
$thisint['outucastpkts'] = $ifXEntry[$intid][11];
$thisint['indiscards'] = $thisint[13];
$thisint['outdiscards'] = $thisint[19];
$thisint['inerrors'] = $thisint[14];
$thisint['outerrors'] = $thisint[20];
$thisint['inmulticast'] = $ifXEntry[$intid][8];
$thisint['outmulticast'] = $ifXEntry[$intid][12];
$thisint['inbroadcast'] = $ifXEntry[$intid][9];
$thisint['outbroadcast'] = $ifXEntry[$intid][13];
$thisint['alias'] = $ifXEntry[$intid][18];
# Sanitise the name
$intname = str_replace("/", "-", $thisint[2]);
$intname = str_replace(" ", "-", $intname);
$intname = str_replace(":", "-", $intname);
$intname = str_replace('"', "", $intname);
$thisint['name'] = $intname;
# Sanitise the alias
$thisint['alias'] = str_replace('"', "", $thisint['alias']);
logline("{$pollprettyhost} - {$intname} - Description for {$intname} is {$thisint['alias']}.", 2, $verbose);
# Send data to carbon.
if ($carbon) {
foreach($graphite_metrics as $metric) {
if ($thisint[$metric]) {
fwrite($carbon, "{$graphite_prefix}.{$graphite_datacenter}-{$pollprettyhost}.{$thisint['name']}.{$metric} {$thisint[$metric]} {$timestamp}\n");
}
}
}
# This loop is going to run a lot. For every interface, create every graph.
foreach($pollgraphs as $thisgraph) {
logline("{$pollprettyhost} - {$intname} - Starting loop for interface {$intname} and graph type {$thisgraph}", 2, $verbose);
$thisgraphdef = getGraphDefinition($thisgraph);
$genrrdname = "{$pollprettyhost}-{$intname}_{$thisgraphdef['filesuffix']}.rrd";
logline("{$pollprettyhost} - {$intname} - Starting find or create RRD for graphtype {$thisgraph} and interface {$thisint['name']}... ", 2, $verbose);
if (!findOrCreateRRD($genrrdname, $pollprettyhost, $thisgraphdef['rrddef'])) {
logline("{$pollprettyhost} - {$intname} - findOrCreateRRD returned false! Could not find or create the RRD file, check your permissions", 0, $verbose);
return false;
}
logline("{$pollprettyhost} - {$intname} - Find or create rrd done", 2, $verbose);
# This ugly little loop is neccesery to collect up all the data sources, get the right numbers and add a colon in the middle only.
$insertvalues = "";
$i = 0;
$numberofds = count($thisgraphdef['datasources']);
foreach($thisgraphdef['datasources'] as $thisds) {
$insertvalues .= $thisint[$thisds];
if($i < $numberofds - 1) {
$insertvalues .= ":";
}
$i++;
}
logline("{$pollprettyhost} - {$intname} - Going to update RRD {$genrrdname} with data {$insertvalues}", 2, $verbose);
updateRRD($genrrdname, $pollprettyhost, $timestamp, $insertvalues);
logline("{$pollprettyhost} - {$intname} - Update RRD done", 1, $verbose);
logline("{$pollprettyhost} - {$intname} - Updating database", 2, $verbose);
# Insert the details of this graph/port into the database for future reference
connectToDB();
# first, delete the previous row if it exists
mysql_query('DELETE FROM ports where host="' . $pollprettyhost . '" AND safename="' . $thisint['name']. '" AND graphtype="' . $thisgraph . '"');
# Now insert the values
mysql_query('INSERT INTO ports (host, name, safename, filename, alias, graphtype, lastpoll)
VALUES ("'.$pollprettyhost.'", "'.$thisint[2].'", "'.$thisint['name'].'", "'.$genrrdname.'", "'.$thisint['alias'].'", "'.$thisgraph.'", "'. $timestamp .'")');
logline("{$pollprettyhost} - {$intname} - Done Updating database", 2, $verbose);
logline("{$pollprettyhost} - {$intname} - Done loop for interface {$thisint['name']} and graph type {$thisgraph}", 2, $verbose);
}
} else {
logline("{$pollprettyhost} - Interface {$thisint['name']} was either admin down or oper down, not polling this run", 1, $verbose);
}
logline("{$pollprettyhost} - {$intname} - Loop for interface {$thisint['name']} complete", 1, $verbose);
}
logline("{$pollprettyhost} - Poller has completed it's run for this host. ", 0, $verbose);
# Disconnect carbon.
if ($carbon) {
fclose($carbon);
}