|
<?php |
|
// header('Content-Type: application/json; charset=utf-8'); |
|
|
|
|
|
include("session.inc"); |
|
include("amifunctions.inc"); |
|
|
|
// Modified by: John Hogenmiller KB3DFZ December 28, 2022 |
|
// This is a fork of rptstats.php - it only works with nodes defined in the |
|
// allmon.ini file and by default returns prometheus metrics output for each key. This file should |
|
// be saved to your supermon directoy alongside rptstats.php |
|
// Also updated to match supermon 6.2.2 AMICommand. |
|
|
|
// Author: Paul Aidukas KN2R (Copyright) July 15, 2013 |
|
// For ham radio use only, NOT for comercial use! |
|
// Be sure to allow popups from your Allmon web server to your browser!! |
|
// Major update by KB4FXC 02/2018 |
|
|
|
// if you pass in any debug=, then debug/echo everything |
|
if (isset($_GET["debug"])) |
|
{ |
|
$debug = true; |
|
} |
|
else |
|
{ |
|
$debug = false; |
|
} |
|
|
|
|
|
if (isset($_GET["format"])) |
|
{ |
|
$format = strtolower(trim(strip_tags($_GET['format']))); |
|
} |
|
else |
|
{ |
|
$format = "metrics"; |
|
} |
|
|
|
$node = (int)trim(strip_tags($_GET['node'])); |
|
|
|
if (!file_exists('allmon.ini')) { |
|
$r = array("node" => $node, "error" => "Could not load allmon ini file."); |
|
die(json_encode($r)); |
|
} |
|
|
|
// Read supermon INI file |
|
|
|
$config = parse_ini_file('allmon.ini', true); |
|
|
|
if (!isset($config[$node])) // Check if node exists in ini |
|
die(json_encode(array("node" => $node, "error" => "Node ${node} is not in allmon ini file"))); |
|
|
|
if (($fp = AMIconnect($config[$node]['host'])) === FALSE) // Set up Asterisk manager connection |
|
die(json_encode(array("node" => $node, "error" => "Could not connect to Asterisk Manager."))); |
|
|
|
|
|
if (AMIlogin($fp, $config[$node]['user'], $config[$node]['passwd']) === FALSE) // Login to Asterisk manager |
|
die(json_encode(array("node" => $node, "error" => "Could not login to Asterisk Manager."))); |
|
|
|
|
|
$results = get_rpt_stats($fp, $node, $debug); |
|
if ($format === "metrics") { |
|
header('Content-Type: text/plain; charset=utf-8'); |
|
output_prometheus_stats($results); |
|
} else { |
|
header('Content-Type: application/json; charset=utf-8'); |
|
echo json_encode($results); |
|
} |
|
|
|
function get_rpt_stats($fp, $node, $debug) |
|
{ |
|
|
|
$results = array("title"=>"Allstar rpt stats for node {$node}"); |
|
$results["node"] = $node; |
|
$results["allstar"] = "http://stats.allstarlink.org/nodeinfo.cgi?node={$node}"; |
|
|
|
$timeups = array("tx_time_today", "tx_time_since_system_initialization", "uptime"); |
|
|
|
|
|
$AMI1 = AMIcommand ($fp, false, "rpt stats $node"); |
|
|
|
$stats = trim (`echo -n "$AMI1" | head --lines=-1`); |
|
|
|
// $AMI1 = AMIcommand ($fp, "rpt stats $node"); |
|
|
|
// $stats = trim (`echo -n "$AMI1" | head --lines=-1`); |
|
if ($debug) { echo "debug stats {$stats}\n\n"; } |
|
if ("$stats") { |
|
$lines = explode(PHP_EOL, $stats); |
|
if ($debug) { echo "debug lines {$stats}\n"; } |
|
|
|
foreach ($lines as $line) { |
|
if ($debug) { |
|
echo "debug: line {$line}\n"; |
|
} |
|
$stat = explode(":", $line); |
|
$k = strtolower(trim($stat[0])); |
|
$k = preg_replace("/[^a-zA-Z0-9\s]/", "", $k); |
|
$k = preg_replace("/ /", "_", $k); |
|
$v = trim($stat[1]); |
|
|
|
|
|
|
|
// some keys are empty. Ignore and continue on to next line |
|
if (empty($k)) { |
|
continue; |
|
} |
|
|
|
// some lines (timeups) are DD:HH:MM:SS count ups |
|
|
|
if (in_array($k, $timeups)) { |
|
$time = trim("$stat[1]:$stat[2]:$stat[3]:$stat[4]"); |
|
|
|
$time = rtrim($time,':'); |
|
if ($k == "uptime") { |
|
$seconds = mktime($stat[1],$stat[2],$stat[3],1,1,1970); |
|
} |
|
else { |
|
$hours = intval(trim($stat[1])); |
|
$minutes = intval(trim($stat[2])); |
|
$seconds = intval(trim($stat[3])); |
|
$longtime = "{$hours} hours, {$minutes} minutes, {$seconds} seconds"; |
|
$seconds = strtotime("1970-01-01 $longtime UTC"); |
|
} |
|
// echo "\"$k\": $seconds,"; |
|
// echo "\n"; |
|
$results["{$k}"] = $seconds; |
|
|
|
// continue to next line |
|
continue; |
|
} |
|
|
|
// todo "nodes_currently_connected_to_us": "533172, 51461, 533170, 533171" |
|
// if we can parse that field into an array and count the length, that |
|
// would be great. We might also be able to fetch the node frequency, |
|
// description, and more from the node database. |
|
if ($k === "nodes_currently_connected_to_us") { |
|
// convert csv list to array of integer values |
|
$n = array_map('intval', explode(",", $v)); |
|
$results["nodes_currently_connected_to_us"] = $n; |
|
$results["nodes_currently_connected_to_us_count"] = count($n); |
|
// continue on to next line |
|
continue; |
|
} |
|
|
|
|
|
if (is_numeric($v)) { |
|
// cheat to handle int or float |
|
$v = $v + 0; |
|
$results[$k] = $v; |
|
if ($debug) { echo "numeric\n"; } |
|
} else { |
|
if ($debug) { echo "stringable\n"; } |
|
$results[$k] = "{$v}"; |
|
} |
|
if ($debug) { echo "end of loop\n"; } |
|
} // end for-each |
|
} else { |
|
// echo htmlspecialchars("<NONE>") . "\n"; |
|
$results["error"] = "No stats found"; |
|
// $results["allstar"] = "http://stats.allstarlink.org/nodeinfo.cgi?node={$node}"; |
|
|
|
} |
|
|
|
// echo json_encode($results); |
|
return $results; |
|
|
|
} |
|
|
|
function output_prometheus_stats($stats) { |
|
|
|
// rpt_up{node="533173"} 1 |
|
// rpt_title{node="533173"} Allstar rpt stats for node 533173 |
|
// rpt_node{node="533173"} 533173 |
|
// rpt_allstar{node="533173"} http://stats.allstarlink.org/nodeinfo.cgi?node=533173 |
|
// rpt_node_533173_statistics_{node="533173"} |
|
// rpt_selected_system_state{node="533173"} 0 |
|
// rpt_signal_on_input{node="533173"} NO |
|
// rpt_system{node="533173"} ENABLED |
|
// rpt_parrot_mode{node="533173"} DISABLED |
|
// rpt_scheduler{node="533173"} ENABLED |
|
// rpt_tail_time{node="533173"} STANDARD |
|
// rpt_time_out_timer{node="533173"} ENABLED |
|
// rpt_incoming_connections{node="533173"} ENABLED |
|
// rpt_time_out_timer_state{node="533173"} RESET |
|
// rpt_time_outs_since_system_initialization{node="533173"} 2 |
|
// rpt_identifier_state{node="533173"} CLEAN |
|
// rpt_kerchunks_today{node="533173"} 68 |
|
// rpt_kerchunks_since_system_initialization{node="533173"} 6225 |
|
// rpt_keyups_today{node="533173"} 68 |
|
// rpt_keyups_since_system_initialization{node="533173"} 7180 |
|
// rpt_dtmf_commands_today{node="533173"} 0 |
|
// rpt_dtmf_commands_since_system_initialization{node="533173"} 4 |
|
// rpt_last_dtmf_command_executed{node="533173"} 81 |
|
// rpt_tx_time_today{node="533173"} 833 |
|
// rpt_tx_time_since_system_initialization{node="533173"} |
|
// rpt_uptime{node="533173"} 2597720 |
|
// rpt_nodes_currently_connected_to_us{node="533173"} Array |
|
// rpt_nodes_currently_connected_to_us_count{node="533173"} 4 |
|
// rpt_autopatch{node="533173"} ENABLED |
|
// rpt_autopatch_state{node="533173"} DOWN |
|
// rpt_autopatch_called_number{node="533173"} N/A |
|
// rpt_reverse_patchiaxrpt_connected{node="533173"} DOWN |
|
// rpt_user_linking_commands{node="533173"} ENABLED |
|
// rpt_user_functions{node="533173"} ENABLED |
|
|
|
|
|
$node = $stats["node"]; |
|
echo "up{node=\"{$node}\"} 1\n"; |
|
echo "rpt_up{node=\"{$node}\"} 1\n"; |
|
foreach($stats AS $k => $v) { |
|
$k = ltrim($k, "_"); |
|
$name = "rpt_{$k}"; |
|
// convert up/down things to 1/0 |
|
if (in_array($v, ["ENABLED", "UP", "CLEAN", "YES"])) { $v = 1; } |
|
if (in_array($v, ["DISABLED", "DOWN", "NO"])) { $v = 0; } |
|
// convert any text value to 1, include text as a label |
|
if (is_numeric($v)) { |
|
echo "rpt_{$k}{node=\"{$node}\"} {$v}\n"; |
|
} else { |
|
echo "rpt_{$k}{node=\"{$node}\", text=\"{$v}\"} 1\n"; |
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
?> |