Cacti ss get snmp
Views:
This is a script that will collect arbitary snmp data for cacti's script host. You specificy which oids to query in named sections of an ini file, and pass the name of the particular dataset you want in as an argument when calling the script.
This means you can start collecting and graphing data for new things on your network with out necessarily needing to write a new script to get the data.
This script is based on the work of the Cacti developers and contributors to the cacti project. It's licensed under the same GNU General Public License.
<?php
// Cacti Script Server boiler-plate:
/* 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>");
}
$no_http_headers = true;
/* display No errors */
ini_set('display_errors', 1);
if (isset($config)) {
include_once(dirname(__FILE__) . "/../lib/snmp.php");
}
if (!isset($called_by_script_server)) {
include_once(dirname(__FILE__) . "/../include/global.php");
include_once(dirname(__FILE__) . "/../lib/snmp.php");
array_shift($_SERVER["argv"]);
$functionName = $_SERVER["argv"][0];
array_shift($_SERVER["argv"]);
print call_user_func_array($functionName, $_SERVER["argv"]);
}
/*
* SETTINGS
*/
define('SS_GET_SNMP_INI', '/etc/cacti/ss_get_snmp.ini');
// http://www.php.net/manual/en/function.sys-get-temp-dir.php#85261
if (!function_exists("sys_get_temp_dir")) {
function sys_get_temp_dir() {
if( $temp=getenv('TMP') ) return $temp;
if( $temp=getenv('TEMP') ) return $temp;
if( $temp=getenv('TMPDIR') ) return $temp;
$temp=tempnam(__FILE__,'');
if (file_exists($temp)) {
unlink($temp);
return dirname($temp);
}
return null;
}
}
function ss_get_snmp_config($query) {
static $config;
if (!$config) {
$config = parse_ini_file(SS_GET_SNMP_INI, true);
}
return (is_array($config) && isset($config[$query])) ? $config[$query] : null;
}
/**
* Query a group of snmp oids and specified in the ini configuration file for this script
*
* @param string $query a query name specified in the ss_get_snmp.ini configuration file
* @param string $hostname the hostname or ipaddress of the snmp server
* @param string $snmpauth colon delimited list of snmp connection parameters
* @return null
**/
function ss_get_snmp($query, $hostname, $snmpauth)
{
$results = null;
$walk = false;
$oids = ss_get_snmp_config($query);
if (!$oids) {
trigger_error("Not such query: $query", E_USER_WARNING);
return null;
}
if (isset($oids['__walk']) && $oids['__walk']) {
$walk = true;
unset($oids['__walk']);
}
$polling_interval = read_config_option("poller_interval");
$temp_dir = sys_get_temp_dir() . "/" . basename(__FILE__);
$temp_dir = preg_replace('/\.php$/', '', $temp_dir);
if (!is_dir($temp_dir)) {
mkdir($temp_dir);
}
if (!is_dir($temp_dir."/$query")) {
mkdir($temp_dir."/$query");
}
$temp_dir = "$temp_dir/$query";
if (is_file("$temp_dir/$hostname")) {
$cache_stats = stat("$temp_dir/$hostname");
if ( (($cache_stats['mtime'] + $polling_interval) * 0.75) > time() ) {
if ($cache = fopen("$temp_dir/$hostname", 'r')) {
while (!feof($cache)) {
$results .= fgets($cache);
}
}
}
}
if (strlen($results)) {
return $results;
}
$results = ss_get_snmp_query($oids, $hostname, $snmpauth, $walk);
if (is_file("$temp_dir/$hostname") && !is_writable("$temp_dir/$hostname")) {
unlink("$temp_dir/$hostname");
}
file_put_contents("$temp_dir/$hostname", $results);
return $results;
//return (strlen($results)) ? true : false;
}
/**
* generic function to fetch a list of oids and output the results
* in cacti scripts servers fieldname:value format
*
* @param array $oids an assiative array of oids to query. The array keys are used as the field names for cacti in the results.
* @param string $hostname the hostname or ipaddress of the snmp server
* @param string $snmpauth colon delimited list of snmp connection parameters
* @param bool $walk if true, use snmp walk to fetch data, otherwise use snmp get
*
* @return string
**/
function ss_get_snmp_query($query, $hostname, $snmpauth, $walk=false)
{
$snmp = explode(":", $snmpauth);
$snmp_version = $snmp[0];
$snmp_port = $snmp[1];
$snmp_timeout = $snmp[2];
$ping_retries = (strlen($snmp[3]) ? $snmp[3] : 0);
$max_oids = (strlen($snmp[4]) ? $snmp[4] : 0);
$snmp_auth_username = "";
$snmp_auth_password = "";
$snmp_auth_protocol = "";
$snmp_priv_passphrase = "";
$snmp_priv_protocol = "";
$snmp_context = "";
$snmp_community = "";
if ($snmp_version == 3)
{
$snmp_auth_username = isset($snmp[6]) ? $snmp[6] : "";
$snmp_auth_password = isset($snmp[7]) ? $snmp[7] : "";
$snmp_auth_protocol = isset($snmp[8]) ? $snmp[8] : "";
$snmp_priv_passphrase = isset($snmp[9]) ? $snmp[9] : "";
$snmp_priv_protocol = isset($snmp[10]) ? $snmp[10] : "";
$snmp_context = isset($snmp[11]) ? $snmp[11] : "";
}
else
{
$snmp_community = isset($snmp[5]) ? $snmp[5] : "";
}
$output = '';
$cacti_snmp_func = $walk ? "cacti_snmp_walk" : "cacti_snmp_get";
foreach ($query as $name => $oid)
{
$results = $cacti_snmp_func($hostname, $snmp_community,
$oid, $snmp_version, $snmp_auth_username,
$snmp_auth_password, $snmp_auth_protocol,
$snmp_priv_passphrase, $snmp_priv_protocol, $snmp_context,
$snmp_port, $snmp_timeout, $ping_retries, $max_oids,
SNMP_POLLER
);
if ($walk)
{
for ($i = 0; $i < sizeof($results); ++$i)
{
$output .= $name . ($walk ? ($i + 1) : null) . ":" .
$results[$i]['value'] . " ";
}
}
else
{
$output .= "$name:$results ";
}
}
return trim($output);
}
