-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.php
233 lines (186 loc) · 5.75 KB
/
cli.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/php -q
<?php
include(dirname(__FILE__) . '/../../include/cli_check.php');
include(dirname(__FILE__) . '/config.php');
require_once($config['base_path'] . '/plugins/npc/controllers/hosts.php');
require_once($config['base_path'] . '/plugins/npc/controllers/hostgroups.php');
require_once($config['base_path'] . '/plugins/npc/controllers/services.php');
/* process calling arguments */
$parms = $_SERVER['argv'];
array_shift($parms);
$listHosts = null;
$listHostgroups = null;
$listServices = null;
$getPerfHistory = null;
$getPerfData = null;
$begin = null;
$end = null;
$objectId = null;
$serviceName = null;
$hostname = null;
$type = null;
if (!sizeof($parms)) {
display_help();
exit(0);
} else {
foreach ($parms as $parameter) {
@list($arg, $value) = @explode('=', $parameter);
switch ($arg) {
case '--type':
$type = trim($value);
break;
case '--id':
$objectId = trim($value);
break;
case '--service':
$serviceName = trim($value);
break;
case '--host':
$hostname = trim($value);
break;
case '--hostgroup':
$hostgroup = trim($value);
break;
case '--ds':
$ds = trim($value);
break;
case '--list-hosts':
$listHosts = 1;
break;
case '--list-hostgroups':
$listHostgroups = 1;
break;
case '--list-services':
$listServices = 1;
break;
case '--perfdata':
$getPerfData = 1;
break;
case '--begin':
$begin = trim($value);
break;
case '--end':
$end = trim($value);
break;
case '--perf-history':
$getPerfHistory = 1;
break;
default:
$getPerfData = 1;
break;
}
}
}
if ($getPerfHistory) { getPerfHistory(); }
if ($getPerfData) { getPerfData(); }
if ($listHosts) { listHosts(); }
if ($listHostgroups) { listHostgroups(); }
if ($listServices) { listServices(); }
function getPerfHistory() {
global $hostname, $serviceName, $begin, $end;
if ($serviceName) {
$class = 'NpcServicesController';
} else {
$class = 'NpcHostsController';
}
$obj = new $class;
$results = $obj->getPerfHistory($hostname, $serviceName, $begin, $end);
$count = count($results);
for ($i = 0; $i < $count; $i++) {
//$output = '';
//$perfParts = explode(" ", $results[$i]['perfdata']);
//foreach ($perfParts as $perf) {
// if (preg_match("/=/", $perf)) {
// preg_match("/(\S+)=([-+]?[0-9]*\.?[0-9]+)/", $perf, $matches);
// $output .= $matches[1] . ":" . $matches[2] . " ";
// }
//}
//print $results[$i]['end_time'] . " " . $output . "\n";
print $results[$i]['end_time'] . ' ' . $results[$i]['perfdata'] . "\n";
}
}
function getPerfData() {
global $objectId, $hostname, $serviceName, $type;
if ($type == 'host') {
$class = 'NpcHostsController';
} else {
$class = 'NpcServicesController';
}
$obj = new $class;
$results = $obj->getPerfData($objectId, $hostname, $serviceName);
$perfParts = explode(' ', $results[0]['perfdata']);
$output = '';
foreach ($perfParts as $perf) {
if (preg_match('/=/', $perf)) {
preg_match('/(\S+)=([-+]?[0-9]*\.?[0-9]+)/', $perf, $matches);
if (preg_match('/^iso./', $matches[1])) {
$matches[1] = 'output';
}
$output .= $matches[1] . ':' . $matches[2] . ' ';
}
}
print $output . "\n";
}
function listHosts() {
$parms = $_SERVER['argv'];
foreach ($parms as $parameter) {
@list($arg, $value) = @explode('=', $parameter);
switch ($arg) {
case '--hostgroup':
$hostgroup = trim($value);
break;
}
}
if (isset($hostgroup)) {
$obj = new NpcHostgroupsController;
$results = $obj->listHostsCli($hostgroup);
} else {
$obj = new NpcHostsController;
$results = $obj->listHostsCli();
}
print "\n" . sprintf("%-10s %-30s %-30s\n", 'Id', 'Name', 'Address') . "\n";
foreach ($results as $result) {
print sprintf("%-10s %-30s %-30s\n", $result['id'], $result['name'], $result['address']);
}
exit;
}
function listHostgroups() {
$obj = new NpcHostgroupsController;
$results = $obj->listHostgroupsCli();
print "\n" . sprintf("%-10s %-30s\n", 'Id', 'Name') . "\n";
foreach ($results as $result) {
print sprintf("%-10s %-30s\n", $result['id'], $result['name']);
}
exit;
}
function listServices() {
$parms = $_SERVER['argv'];
foreach ($parms as $parameter) {
@list($arg, $value) = @explode('=', $parameter);
switch ($arg) {
case '--host':
$hostname = trim($value);
break;
}
}
$obj = new NpcServicesController;
$results = $obj->listServicesCli($hostname);
$x = 1;
print "\n" . sprintf("%-10s %-20s %-30s\n", 'ID', 'Host', 'Service') . "\n";
foreach ($results as $result) {
print sprintf("%-10s %-20s %-30s\n", $result['service_object_id'], $result['host'], $result['display_name']);
}
exit;
}
function display_help() {
print "A simple command line utility to fetch host or service performance data from NPC\n\n";
print "usage: perfdata.php --type=[host|service] --id=[ID] --datasource=[DS]\n\n";
print "Required:\n";
print " --type Type specifies that we are querting perfdata for a host or service\n";
print " --id The host or service object ID\n";
print "Optional:\n";
print " --ds Return only the specified datasource. All returned by default.\n";
print "List Options:\n";
print " --list-hosts\n";
print " --list-services\n\n";
}