Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infotek/afca555ae101168bf91877efb32644ec to your computer and use it in GitHub Desktop.
Save infotek/afca555ae101168bf91877efb32644ec to your computer and use it in GitHub Desktop.
<?php
// TEST SCRIPT much better fix
// $command = '/usr/lib/nagios/plugins/check_mysql -u user -p password ' ;
// This array is used to test for valid UOM's to be used for graphing.
// Valid values from: https://nagios-plugins.org/doc/guidelines.html#AEN200
// Note: This array must be decend from 2 char to 1 char so that the search works correctly.
$valid_uom = array ('us', 'ms', 'KB', 'MB', 'GB', 'TB', 'c', 's', '%', 'B');
// Make our command safe.
// $command = escapeshellcmd($command);
// Run the command and return its response.
// exec($command, $response_array, $status);
// exec returns an array, lets implode it back to a string.
// $response_string = implode("\n", $response_array);
$response_string = 'Uptime: 4510 Threads: 5 Questions: 5056 Slow queries: 0 Opens: 102 Flush tables: 1 Open tables: 165 Queries per second avg: 1.121|Connections01234567=0;;; Connections0123456789=1;;; Connections0123456789=2;;; Connections0123456789=3;;; Connections0123456789=4;;; Open_files=60;;; Open_tables=165;;; Qcache_free_memory=16482416;;; Qcache_hits=2600c;;; Qcache_inserts=615c;;; Qcache_lowmem_prunes=0c;;; Qcache_not_cached=201c;;; Qcache_queries_in_cache=155;;; Queries=5056c;;; Questions=5056c;;; Table_locks_waited=0c;;; Threads_connected=5;;; Threads_running=1;;; Uptime=4510c;;;';
// Split out the response and the performance data.
list($response, $perf) = explode("|", $response_string);
// Split each performance metric
$perf_arr = explode(' ', $perf);
// Create an array for our metrics.
$metrics = array();
echo("\n\$perf_arr\n");
print_r($perf_arr);
// Loop through the perf string extracting our metric data
foreach ($perf_arr as $string) {
// Separate the DS and value: DS=value
list ($ds,$values) = explode('=', trim($string));
// Keep the first value, discard the others.
list($value,,,) = explode(';', trim($values));
$value = trim($value);
// Set an empty uom
$uom = '';
// is the UOM valid - https://nagios-plugins.org/doc/guidelines.html#AEN200
foreach ($valid_uom as $v) {
if ((strlen($value)-strlen($v)) === strpos($value, $v)) {
// Yes, store and strip it off the value
$uom = $v;
$value = substr($value, 0, -strlen($v));
break;
}
}
if ($ds != "") {
// Normalize ds for rrd : ds-name must be 1 to 19 characters long in the characters [a-zA-Z0-9_]
// http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html
$normalized_ds = preg_replace('/[^a-zA-Z0-9_]/', '', $ds);
// if ds_name is longer than 19 characters, only use the first 19
if (strlen($normalized_ds) > 19) {
$normalized_ds = substr($normalized_ds, 0, 19);
echo($ds . " exceeded 19 characters, renaming to " . $normalized_ds . "\n");
}
if ($ds != $normalized_ds) {
// ds has changed. check if normalized_ds is already in the array
if (isset($metrics[$normalized_ds])) {
echo($normalized_ds . " collides with an existing index\n");
$perf_unique = 0;
// Try to generate a unique name
for ($i = 0; $i<10; $i++) {
$tmp_ds_name = substr($normalized_ds, 0, 18) . $i;
if (!isset($metrics[$tmp_ds_name])) {
echo($normalized_ds . " collides with an existing index\n");
$normalized_ds = $tmp_ds_name;
$perf_unique = 1;
break 1;
}
}
if ($perf_unique == 0) {
// Try harder to generate a unique name
for ($i = 0; $i<10; $i++) {
for ($j = 0; $j<10; $j++) {
$tmp_ds_name = substr($normalized_ds, 0, 17) . $j . $i;
if (!isset($perf[$tmp_ds_name])) {
$normalized_ds = $tmp_ds_name;
$perf_unique = 1;
break 2;
}
}
}
}
if ($perf_unique == 0) {
echo("could not generate a unique ds-name for " . $ds . "\n");
}
}
$ds = $normalized_ds ;
}
// We have a DS. Add an entry to the array.
echo("Perf Data - DS: ".$ds.", Value: ".$value.", UOM: ".$uom."\n");
$metrics[$ds] = array ('value'=>$value, 'uom'=>$uom);
} else {
// No DS. Don't add an entry to the array.
echo("Perf Data - None.\n");
}
}
echo("\$metrics\n");
print_r($metrics);
return array ($status, $response, $metrics);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment