-
Notifications
You must be signed in to change notification settings - Fork 3
/
perfdata.php
171 lines (134 loc) · 4.42 KB
/
perfdata.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
#!/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);
$objectId = null;
$serviceName = null;
$hostname = 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();
break;
case '--list-hostgroups':
listHostgroups();
break;
case '--list-services':
listServices();
break;
}
}
}
if ($type == 'host') {
$class = 'NpcHostsController';
} else {
$class = 'NpcServicesController';
}
// perfdata.php --type=service --host=<hostname> --service='System: CPU'
$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] . ' ';
}
}
echo $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 ($hostgroup) {
$obj = new NpcHostgroupsController;
$results = $obj->listHostsCli($hostgroup);
} else {
$obj = new NpcHostsController;
$results = $obj->listHostsCli();
}
echo "ID Name Address\n";
foreach($results as $result) {
echo $result['id'] . " " . $result['name'] . " " . $result['address'] . "\n";
}
exit;
}
function listHostgroups() {
$obj = new NpcHostgroupsController;
$results = $obj->listHostgroupsCli();
echo "ID Name\n";
foreach($results as $result) {
echo $result['id'] . " " . $result['name'] . "\n";
}
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;
echo "\n" . sprintf("%-30s %-30s %-30s %-30s\n", 'Instance', 'Host', 'Service', 'Service Object ID') . "\n";
foreach($results as $result) {
echo sprintf("%-30s %-30s %-30s %-30s\n", $result['instance'], $result['host'], $result['display_name'], $result['service_object_id']);
}
exit;
}
function display_help() {
echo "A simple command line utility to fetch host or service performance data from NPC\n\n";
echo "usage: perfdata.php --type=[host|service] --id=[ID] --datasource=[DS]\n\n";
echo "Required:\n";
echo " --type Type specifies that we are querting perfdata for a host or service\n";
echo " --id The host or service object ID\n";
echo "Optional:\n";
echo " --ds Return only the specified datasource. All returned by default.\n";
echo "List Options:\n";
echo " --list-hosts [--hostgroup=<name>]\n";
echo " --list-hostgroups\n";
echo " --list-services\n\n";
}