Skip to content

Commit

Permalink
added wmi scripting and templates to pull in network traffic information
Browse files Browse the repository at this point in the history
  • Loading branch information
grilled-cheese committed Jul 11, 2014
1 parent df4ab20 commit f54dd52
Show file tree
Hide file tree
Showing 6 changed files with 1,550 additions and 0 deletions.
119 changes: 119 additions & 0 deletions query_wmi_netstats.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/* do NOT run this script through a web browser */
if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD']) || isset($_SERVER['REMOTE_ADDR'])) {
die("<br><strong>This script is only meant to run at the command line.</strong>");
}

# deactivate http headers
$no_http_headers = true;
# include some cacti files for ease of use
include(dirname(__FILE__) . "/../include/global.php");
include(dirname(__FILE__) . "/../lib/snmp.php");

# define all OIDs we need for further processing
$oids = array(
"index" => ".1.3.6.1.2.1.2.2.1.1",
);
$xml_delimiter = "!";

# all required input parms
$hostname = $_SERVER["argv"][1];
$wmi_cred = $_SERVER["argv"][2];
$cmd = $_SERVER["argv"][3];
if (isset($_SERVER["argv"][4])) { $query_field = $_SERVER["argv"][4]; };
if (isset($_SERVER["argv"][5])) { $query_index = $_SERVER["argv"][5]; };

# get number of snmp retries from global settings
$snmp_retries = read_config_option("snmp_retries");

# -------------------------------------------------------------------------
# main code starts here
# -------------------------------------------------------------------------


# -------------------------------------------------------------------------
# script MUST respond to index queries
# the command for this is defined within the XML file as
# <arg_index>index</arg_index>
# you may replace the string "index" both in the XML and here
# -------------------------------------------------------------------------
# php -q <script> <parms> index
# will list all indices of the target values
# e.g. in case of interfaces
# it has to respond with the list of interface indices
# -------------------------------------------------------------------------
function get_netstats($host,$cred) {
$phpExec = trim(`which php`);
if(empty($phpExec)) {
echo "Could not find php executable!\n";
exit;
}
$path = substr(__FILE__, 0, strlen(__FILE__) - strlen(basename(__FILE__)));
$phpExecQuery = $phpExec." ".$path."wmi.netstats.php -h ".$host." -u ".$cred." 2>/dev/null";
exec($phpExecQuery,$out_netstats,$execstatus);
$dbug = 0;
if ($execstatus != 0) {
$dbug = max($dbug, 1);
echo "\nReturn code non-zero, debug mode enabled in query!\n";
}

if ($dbug == 1) { // basic debug, show output in easy to read format and display the exact execution command
echo "\n".$wmiexec."\nExec Status: ".$execstatus."\n";
$sep = "\n";
}
$temp_netstat_rows = $out_netstats;
$result = array();
foreach($temp_netstat_rows as $row_key => $row) {
$keypairs = explode(" ",$row);
$result[$row_key] = array();
foreach($keypairs as $keypair) {
list($k,$v) = explode(":",$keypair);
$result[$row_key] = array_merge($result[$row_key],array($k => $v));
}
}
return $result;
}

if ($cmd == "index") {
# retrieve all indices from target
# $return_arr = reindex(cacti_snmp_walk($hostname, $snmp_community, $oids["index"], $snmp_version, $snmp_auth_username,
# $snmp_auth_password, $snmp_auth_protocol, $snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context, $snmp_port, $snmp_timeout, $snmp_retries, $max_oids, SNMP_POLLER));

$return_arr = reindex(get_netstats($hostname,$wmi_cred),"adapter_Index");

# and print each index as a separate line
for ($i=0;($i<sizeof($return_arr));$i++) {
print $return_arr[$i] . "\n";
}

# -------------------------------------------------------------------------
}elseif ($cmd == "query" && isset($query_field)) {
$netstats_arr = get_netstats($hostname,$wmi_cred);
$arr_index = reindex($netstats_arr,"adapter_Index");
$arr = reindex($netstats_arr,$query_field);

for ($i=0;($i<sizeof($arr_index));$i++) {
print $arr_index[$i] . $xml_delimiter . $arr[$i] . "\n";
}
# -------------------------------------------------------------------------
}elseif ($cmd == "get" && isset($query_field) && isset($query_index)) {
$netstats_arr = get_netstats($hostname,$wmi_cred);
foreach($netstats_arr as $row_key => $row) {
if(array_key_exists($query_field,$row) && $row['adapter_Index'] == $query_index)
print($row[$query_field]);
}
} else {
print "Invalid use of script query, required parameters:\n\n";
print " <hostname> <cmd>\n";
}

function reindex($arr,$field) {
$return_arr = array();

for ($i=0;($i<sizeof($arr));$i++) {
$return_arr[$i] = $arr[$i][$field];
}

return $return_arr;
}
70 changes: 70 additions & 0 deletions resource/script_queries/wmi_ifTraffic.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<interface>
<name>Get WMI Interface Traffic Information</name>
<script_path>|path_php_binary| -q |path_cacti|/scripts/query_wmi_netstats.php</script_path>
<arg_prepend>|host_hostname| "/etc/cacti/cactiwmi.pw"</arg_prepend>
<arg_index>index</arg_index>
<arg_query>query</arg_query>
<arg_get>get</arg_get>
<output_delimeter>!</output_delimeter>
<index_order>adapter_Index</index_order>
<index_order_type>numeric</index_order_type>
<index_title_format>|chosen_order_field|</index_title_format>

<fields>
<adapter_Index>
<name>Adapter Index</name>
<direction>input</direction>
<query_name>adapter_Index</query_name>
</adapter_Index>
<config_IPAddress>
<name>IP Address</name>
<direction>input</direction>
<query_name>config_IPAddress</query_name>
</config_IPAddress>
<config_MACAddress>
<name>MAC Address</name>
<direction>input</direction>
<query_name>config_MACAddress</query_name>
</config_MACAddress>
<adapter_Name>
<name>Adapter Name</name>
<direction>input</direction>
<query_name>adapter_Name</query_name>
</adapter_Name>
<adapter_Description>
<name>Adapter Description</name>
<direction>input</direction>
<query_name>adapter_Description</query_name>
</adapter_Description>
<config_Description>
<name>Config Description</name>
<direction>input</direction>
<query_name>config_Description</query_name>
</config_Description>
<adapter_NetConnectionStatus>
<name>Adapter Connection Status</name>
<direction>input</direction>
<query_name>adapter_NetConnectionStatus</query_name>
</adapter_NetConnectionStatus>
<stats_CurrentBandwidth>
<name>Adapter Speed</name>
<direction>input</direction>
<query_name>stats_CurrentBandwidth</query_name>
</stats_CurrentBandwidth>
<stats_BytesReceivedPersec>
<name>Bytes Received Per Second</name>
<direction>output</direction>
<query_name>stats_BytesReceivedPersec</query_name>
</stats_BytesReceivedPersec>
<stats_BytesSentPersec>
<name>Bytes Sent Per Second</name>
<direction>output</direction>
<query_name>stats_BytesSentPersec</query_name>
</stats_BytesSentPersec>
<stats_OutputQueueLength>
<name>Output Queue Length</name>
<direction>output</direction>
<query_name>stats_OutputQueueLength</query_name>
</stats_OutputQueueLength>
</fields>
</interface>
Loading

0 comments on commit f54dd52

Please sign in to comment.