First we need a function that will work for older versions of PHP as well as older versions of PowerMTA as well.
This is in two parts: First this xml2array function (keeps things simple)
Name this "xmlfunction.php", it lives in cacti/scripts for now, feel free to move it around.
Code: Select all
<?php
function xml2array($url, $get_attributes = 1, $priority = 'tag')
{
$contents = "";
if (!function_exists('xml_parser_create'))
{
return array ();
}
$parser = xml_parser_create('');
if (!($fp = @ fopen($url, 'rb')))
{
return array ();
}
while (!feof($fp))
{
$contents .= fread($fp, 8192);
}
fclose($fp);
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);
if (!$xml_values)
return; //Hmm...
$xml_array = array ();
$parents = array ();
$opened_tags = array ();
$arr = array ();
$current = & $xml_array;
$repeated_tag_index = array ();
foreach ($xml_values as $data)
{
unset ($attributes, $value);
extract($data);
$result = array ();
$attributes_data = array ();
if (isset ($value))
{
if ($priority == 'tag')
$result = $value;
else
$result['value'] = $value;
}
if (isset ($attributes) and $get_attributes)
{
foreach ($attributes as $attr => $val)
{
if ($priority == 'tag')
$attributes_data[$attr] = $val;
else
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}
if ($type == "open")
{
$parent[$level -1] = & $current;
if (!is_array($current) or (!in_array($tag, array_keys($current))))
{
$current[$tag] = $result;
if ($attributes_data)
$current[$tag . '_attr'] = $attributes_data;
$repeated_tag_index[$tag . '_' . $level] = 1;
$current = & $current[$tag];
}
else
{
if (isset ($current[$tag][0]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 2;
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = & $current[$tag][$last_item_index];
}
}
elseif ($type == "complete")
{
if (!isset ($current[$tag]))
{
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data)
$current[$tag . '_attr'] = $attributes_data;
}
else
{
if (isset ($current[$tag][0]) and is_array($current[$tag]))
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
if ($priority == 'tag' and $get_attributes and $attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
}
else
{
$current[$tag] = array (
$current[$tag],
$result
);
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes)
{
if (isset ($current[$tag . '_attr']))
{
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset ($current[$tag . '_attr']);
}
if ($attributes_data)
{
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
}
elseif ($type == 'close')
{
$current = & $parent[$level -1];
}
}
return ($xml_array);
}
?>
This needs to be named "powermta.php"
Code: Select all
<?
/*
powermta stats collector for cacti
part of the mocospace stats desktop
april 2009 - joeldg
*/
include "xmlfunction.php";
function output($part_of_array){
while(list($k1,$v1)=each($part_of_array)){
$key = $k1;
# this should almost always fire off
if(is_array($v1)){
while(list($k2,$v2)=each($v1)){
$key = $k1 . "_" . $k2;
$val = $v2;
if(is_array($v2)){
while(list($k3,$v3)=each($v2)){
$key = $k1 . "_" . $k2 . "_" . $k3;
$val = $v3;
echo "$key:$val ";
}
}else{
echo "$key:$val ";
}
}
}else{
echo "$key:$v1 ";
}
}
}
$host = $argv[1];
$what = $argv[2] ? $argv[2] : "traffic"; #default to traffic
$port = $argv[3] ? $argv[3] : 8080; # default to 8080 if second arg not present
$depth = array();
if(!$host){
die("\nUSAGE: php {file} {hostname} {traffic|conn|resolver|queue|spool} (?{port})\n");
}
$url = "http://$host:$port/status?format=xml";
$parts = xml2array($url);
# now output it
echo output( $parts['rsp']['data']['status'][$what] );
?>
It looks roughly like this when using it from command line
Code: Select all
# php powermta.php
USAGE: php {file} {hostname} {traffic|conn|resolver|queue|spool} (?{port})
# php powermta.php pmta01 queue
smtp_rcp:0 smtp_dom:0 smtp_kb:0.0 pipe_rcp:0 pipe_dom:0 pipe_kb:0.0 discard_rcp:0 discard_dom:0 discard_kb:0.0 file_rcp:0 file_dom:0 file_kb:0.0 gmImprinter_rcp:0 gmImprinter_dom:0 gmImprinter_kb:0.0 alias_rcp:0 alias_dom:0 alias_kb:0.0
# php powermta.php pmta01 traffic
total_out_rcp:6 total_out_msg:6 total_out_kb:5.8 total_in_rcp:2 total_in_msg:2 total_in_kb:2.0 total_gmImprinted:0 lastHr_out_rcp:0 lastHr_out_msg:0 lastHr_out_kb:0.0 lastHr_in_rcp:0 lastHr_in_msg:0 lastHr_in_kb:0.0 lastHr_gmImprinted:0 lastMin_out_rcp:0 lastMin_out_msg:0 lastMin_out_kb:0.0 lastMin_in_rcp:0 lastMin_in_msg:0 lastMin_in_kb:0.0 lastMin_gmImprinted:0 topPerHr_out_rcp:2 topPerHr_out_msg:2 topPerHr_out_kb:2.4 topPerHr_in_rcp:2 topPerHr_in_msg:2 topPerHr_in_kb:2.0 topPerHr_gmImprinted:0 topPerMin_out_rcp:1 topPerMin_out_msg:1 topPerMin_out_kb:1.2 topPerMin_in_rcp:1 topPerMin_in_msg:1 topPerMin_in_kb:1.0 topPerMin_gmImprinted:0
To add to cacti:
Import the host templates, it includes the data queries, graph templates, and host template.
Hope this helps someone
-joeldg