Skip to content

Instantly share code, notes, and snippets.

@mitchellrj
Created June 3, 2014 13:27
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 mitchellrj/0c11fd9bc52a412fff5b to your computer and use it in GitHub Desktop.
Save mitchellrj/0c11fd9bc52a412fff5b to your computer and use it in GitHub Desktop.
System Uptime SNMP monitoring with Cacti

Installation & Usage

  1. Download this gist as a .zip file by clicking the button to the left labelled "Download Gist"
  2. Unzip the downloaded file.
  3. Copy snmp_uptime.xml into your script_server folder. This is located at /usr/share/cacti/resource/script_server/ on Debian. Make sure it is readable by your Apache/Cacti user.
  4. Copy ss_uptime.php into your scripts folder. This is located at /usr/share/cacti/site/scripts/ on Debian. Make sure it is readable by your Apache/Cacti user.
  5. Access the Cacti web interface.
  6. Choose "Console" and "Import Templates" from the main menu.
  7. Upload cacti_snmp_uptime_templates.xml using the form.
  8. Choose "Data Queries" from the "Collection Methods" section of the main menu.
  9. Click "Add".
  10. Set the following values:
  • Name: SNMP - Get Uptime
  • Description: Get the system uptime via SNMP
  • XML Path: <path_cacti>/resource/script_server/snmp_uptime.xml
  • Data Input Method: Get Script Server Data (Indexed)
  1. Click "Save".
  2. Under "Associated Graph Templates", click "Add".
  3. Set the following values:
  • Name: SNMP - System Uptime
  • Graph Template: SNMP - System Uptime
  1. Under "Data Sources" make sure that "sysUpTime" and "hrUptime (Uptime)" are set.
  2. Click "Save".
  3. Add the "SNMP Get Uptime" data query to each device profile you wish to use it with.
  4. Create graphs in the usual way.
<cacti>
<hash_0000245da6e852e18e8ec31601ea937b88406c>
<name>SNMP - System Uptime</name>
<graph>
<t_title></t_title>
<title>|host_description| - System Uptime</title>
<t_image_format_id></t_image_format_id>
<image_format_id>1</image_format_id>
<t_height></t_height>
<height>120</height>
<t_width></t_width>
<width>600</width>
<t_slope_mode></t_slope_mode>
<slope_mode>on</slope_mode>
<t_auto_scale></t_auto_scale>
<auto_scale>on</auto_scale>
<t_auto_scale_opts></t_auto_scale_opts>
<auto_scale_opts>2</auto_scale_opts>
<t_auto_scale_log></t_auto_scale_log>
<auto_scale_log></auto_scale_log>
<t_scale_log_units></t_scale_log_units>
<scale_log_units></scale_log_units>
<t_auto_scale_rigid></t_auto_scale_rigid>
<auto_scale_rigid></auto_scale_rigid>
<t_auto_padding></t_auto_padding>
<auto_padding>on</auto_padding>
<t_export></t_export>
<export>on</export>
<t_upper_limit></t_upper_limit>
<upper_limit>100</upper_limit>
<t_lower_limit></t_lower_limit>
<lower_limit>0</lower_limit>
<t_base_value></t_base_value>
<base_value>1000</base_value>
<t_unit_value></t_unit_value>
<unit_value></unit_value>
<t_unit_exponent_value></t_unit_exponent_value>
<unit_exponent_value></unit_exponent_value>
<t_vertical_label></t_vertical_label>
<vertical_label>Days</vertical_label>
</graph>
<items>
<hash_100024aa087ff813bcf162a03178df412a974f>
<task_item_id>hash_080024e8f6b6d6f9c5c5b6fd2441aefc2c74db</task_item_id>
<color_id>00CF00</color_id>
<alpha>FF</alpha>
<graph_type_id>7</graph_type_id>
<consolidation_function_id>1</consolidation_function_id>
<cdef_id>hash_050024ee67f9f7f4fb927856ea847d68fb4a4b</cdef_id>
<value></value>
<gprint_id>hash_060024e9c43831e54eca8069317a2ce8c6f751</gprint_id>
<text_format>Uptime</text_format>
<hard_return></hard_return>
<sequence>1</sequence>
</hash_100024aa087ff813bcf162a03178df412a974f>
<hash_1000246adbccbe1d859a3da53450279bfe5d36>
<task_item_id>hash_080024e8f6b6d6f9c5c5b6fd2441aefc2c74db</task_item_id>
<color_id>0</color_id>
<alpha>FF</alpha>
<graph_type_id>9</graph_type_id>
<consolidation_function_id>4</consolidation_function_id>
<cdef_id>hash_050024ee67f9f7f4fb927856ea847d68fb4a4b</cdef_id>
<value></value>
<gprint_id>hash_060024e9c43831e54eca8069317a2ce8c6f751</gprint_id>
<text_format>Current:</text_format>
<hard_return></hard_return>
<sequence>2</sequence>
</hash_1000246adbccbe1d859a3da53450279bfe5d36>
<hash_100024e2968c84063bd3aaf9f044423f9558ee>
<task_item_id>hash_080024e8f6b6d6f9c5c5b6fd2441aefc2c74db</task_item_id>
<color_id>0</color_id>
<alpha>FF</alpha>
<graph_type_id>9</graph_type_id>
<consolidation_function_id>3</consolidation_function_id>
<cdef_id>hash_050024ee67f9f7f4fb927856ea847d68fb4a4b</cdef_id>
<value></value>
<gprint_id>hash_060024e9c43831e54eca8069317a2ce8c6f751</gprint_id>
<text_format>Maximum:</text_format>
<hard_return></hard_return>
<sequence>3</sequence>
</hash_100024e2968c84063bd3aaf9f044423f9558ee>
<hash_10002494c2698626b5a2d48d2d934dbf4dbbb2>
<task_item_id>hash_080024e8f6b6d6f9c5c5b6fd2441aefc2c74db</task_item_id>
<color_id>00A348</color_id>
<alpha>FF</alpha>
<graph_type_id>4</graph_type_id>
<consolidation_function_id>3</consolidation_function_id>
<cdef_id>hash_050024ee67f9f7f4fb927856ea847d68fb4a4b</cdef_id>
<value></value>
<gprint_id>hash_060024e9c43831e54eca8069317a2ce8c6f751</gprint_id>
<text_format>MAX</text_format>
<hard_return>on</hard_return>
<sequence>4</sequence>
</hash_10002494c2698626b5a2d48d2d934dbf4dbbb2>
</items>
<inputs>
<hash_090024ba96fb7aac0e3d55c68b0f36691bfc9a>
<name>Data Source [sysUpTime]</name>
<description></description>
<column_name>task_item_id</column_name>
<items>hash_000024aa087ff813bcf162a03178df412a974f|hash_000024e2968c84063bd3aaf9f044423f9558ee|hash_00002494c2698626b5a2d48d2d934dbf4dbbb2|hash_0000246adbccbe1d859a3da53450279bfe5d36</items>
</hash_090024ba96fb7aac0e3d55c68b0f36691bfc9a>
</inputs>
</hash_0000245da6e852e18e8ec31601ea937b88406c>
<hash_010024811e4d16fd80e51bfe6abced3a6e54d0>
<name>SNMP - System Uptime</name>
<ds>
<t_name></t_name>
<name>|host_description| - System Uptime</name>
<data_input_id>hash_030024332111d8b54ac8ce939af87a7eac0c06</data_input_id>
<t_rra_id></t_rra_id>
<t_rrd_step></t_rrd_step>
<rrd_step>300</rrd_step>
<t_active></t_active>
<active>on</active>
<rra_items>hash_150024c21df5178e5c955013591239eb0afd46|hash_1500240d9c0af8b8acdc7807943937b3208e29|hash_1500246fc2d038fb42950138b0ce3e9874cc60|hash_150024e36f3adb9f152adfa5dc50fd2b23337e</rra_items>
</ds>
<items>
<hash_080024e8f6b6d6f9c5c5b6fd2441aefc2c74db>
<t_data_source_name></t_data_source_name>
<data_source_name>sysUpTime</data_source_name>
<t_rrd_minimum></t_rrd_minimum>
<rrd_minimum>0</rrd_minimum>
<t_rrd_maximum></t_rrd_maximum>
<rrd_maximum>0</rrd_maximum>
<t_data_source_type_id></t_data_source_type_id>
<data_source_type_id>1</data_source_type_id>
<t_rrd_heartbeat></t_rrd_heartbeat>
<rrd_heartbeat>600</rrd_heartbeat>
<t_data_input_field_id></t_data_input_field_id>
<data_input_field_id>0</data_input_field_id>
</hash_080024e8f6b6d6f9c5c5b6fd2441aefc2c74db>
</items>
<data>
</data>
</hash_010024811e4d16fd80e51bfe6abced3a6e54d0>
<hash_030024332111d8b54ac8ce939af87a7eac0c06>
<name>Get Script Server Data (Indexed)</name>
<type_id>6</type_id>
<input_string></input_string>
<fields>
<hash_070024172b4b0eacee4948c6479f587b62e512>
<name>Index Type</name>
<update_rra></update_rra>
<regexp_match></regexp_match>
<allow_nulls></allow_nulls>
<type_code>index_type</type_code>
<input_output>in</input_output>
<data_name>index_type</data_name>
</hash_070024172b4b0eacee4948c6479f587b62e512>
<hash_07002430fb5d5bcf3d66bb5abe88596f357c26>
<name>Index Value</name>
<update_rra></update_rra>
<regexp_match></regexp_match>
<allow_nulls></allow_nulls>
<type_code>index_value</type_code>
<input_output>in</input_output>
<data_name>index_value</data_name>
</hash_07002430fb5d5bcf3d66bb5abe88596f357c26>
<hash_07002431112c85ae4ff821d3b288336288818c>
<name>Output Type ID</name>
<update_rra></update_rra>
<regexp_match></regexp_match>
<allow_nulls></allow_nulls>
<type_code>output_type</type_code>
<input_output>in</input_output>
<data_name>output_type</data_name>
</hash_07002431112c85ae4ff821d3b288336288818c>
<hash_0700245be8fa85472d89c621790b43510b5043>
<name>Output Value</name>
<update_rra>on</update_rra>
<regexp_match></regexp_match>
<allow_nulls></allow_nulls>
<type_code></type_code>
<input_output>out</input_output>
<data_name>output</data_name>
</hash_0700245be8fa85472d89c621790b43510b5043>
</fields>
</hash_030024332111d8b54ac8ce939af87a7eac0c06>
<hash_150024c21df5178e5c955013591239eb0afd46>
<name>Daily (5 Minute Average)</name>
<x_files_factor>0.5</x_files_factor>
<steps>1</steps>
<rows>600</rows>
<timespan>86400</timespan>
<cf_items>1|3</cf_items>
</hash_150024c21df5178e5c955013591239eb0afd46>
<hash_1500240d9c0af8b8acdc7807943937b3208e29>
<name>Weekly (30 Minute Average)</name>
<x_files_factor>0.5</x_files_factor>
<steps>6</steps>
<rows>700</rows>
<timespan>604800</timespan>
<cf_items>1|3</cf_items>
</hash_1500240d9c0af8b8acdc7807943937b3208e29>
<hash_1500246fc2d038fb42950138b0ce3e9874cc60>
<name>Monthly (2 Hour Average)</name>
<x_files_factor>0.5</x_files_factor>
<steps>24</steps>
<rows>775</rows>
<timespan>2678400</timespan>
<cf_items>1|3</cf_items>
</hash_1500246fc2d038fb42950138b0ce3e9874cc60>
<hash_150024e36f3adb9f152adfa5dc50fd2b23337e>
<name>Yearly (1 Day Average)</name>
<x_files_factor>0.5</x_files_factor>
<steps>288</steps>
<rows>797</rows>
<timespan>33053184</timespan>
<cf_items>1|3</cf_items>
</hash_150024e36f3adb9f152adfa5dc50fd2b23337e>
<hash_050024ee67f9f7f4fb927856ea847d68fb4a4b>
<name>Ticks to days</name>
<items>
<hash_1400244a0e2533f359440faabdd0bcb42a92a6>
<sequence>1</sequence>
<type>4</type>
<value>CURRENT_DATA_SOURCE</value>
</hash_1400244a0e2533f359440faabdd0bcb42a92a6>
<hash_1400242f8f6cee8a9106fe6273a1f5499afa4b>
<sequence>2</sequence>
<type>6</type>
<value>8640000</value>
</hash_1400242f8f6cee8a9106fe6273a1f5499afa4b>
<hash_1400245a9312e75bb0462f047e8b252db77071>
<sequence>3</sequence>
<type>2</type>
<value>4</value>
</hash_1400245a9312e75bb0462f047e8b252db77071>
</items>
</hash_050024ee67f9f7f4fb927856ea847d68fb4a4b>
<hash_060024e9c43831e54eca8069317a2ce8c6f751>
<name>Normal</name>
<gprint_text>%8.2lf%s</gprint_text>
</hash_060024e9c43831e54eca8069317a2ce8c6f751>
</cacti>
<interface>
<name>Get uptime via SNMP</name>
<script_path>|path_cacti|/scripts/ss_snmp_uptime.php</script_path>
<script_function>ss_snmp_uptime</script_function>
<script_server>php</script_server>
<arg_prepend>|host_hostname| |host_id| |host_snmp_version|:|host_snmp_port|:|host_snmp_timeout|:|host_ping_retries|:|host_max_oids|:|host_snmp_community|:|host_snmp_username|:|host_snmp_password|:|host_snmp_auth_protocol|:|host_snmp_priv_passphrase|:|host_snmp_priv_protocol|:|host_snmp_context|</arg_prepend>
<arg_index>index</arg_index>
<arg_query>query</arg_query>
<arg_get>get</arg_get>
<arg_num_indexes>num_indexes</arg_num_indexes>
<output_delimeter>!</output_delimeter>
<index_order>hrSystemID</index_order>
<index_order_type>numeric</index_order_type>
<index_title_format>System#|chosen_order_field|</index_title_format>
<fields>
<hrSystemID>
<name>System Index Number</name>
<direction>input</direction>
<query_name>index</query_name>
</hrSystemID>
<hrUptime>
<name>Uptime</name>
<direction>output</direction>
<query_name>uptime</query_name>
</hrUptime>
</fields>
</interface>
<?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>");
}
$no_http_headers = true;
/* display No errors */
error_reporting(0);
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"]);
print call_user_func_array("ss_snmp_uptime", $_SERVER["argv"]);
}
function ss_snmp_uptime($hostname, $host_id, $snmp_auth, $cmd, $arg1 = "", $arg2 = "") {
$snmp = explode(":", $snmp_auth);
$snmp_version = $snmp[0];
$snmp_port = $snmp[1];
$snmp_timeout = $snmp[2];
$ping_retries = $snmp[3];
$max_oids = $snmp[4];
$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 = $snmp[6];
$snmp_auth_password = $snmp[7];
$snmp_auth_protocol = $snmp[8];
$snmp_priv_passphrase = $snmp[9];
$snmp_priv_protocol = $snmp[10];
$snmp_context = $snmp[11];
}else{
$snmp_community = $snmp[5];
}
$oids = array(
"index" => ".1.3.6.1.2.1.1.3",
"uptime" => ".1.3.6.1.2.1.1.3"
);
if (($cmd == "index")) {
$arr_index = ss_snmp_uptime_get_indexes($hostname, $snmp_community, $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);
for ($i=0;($i<sizeof($arr_index));$i++) {
print $arr_index[$i] . "\n";
}
}elseif (($cmd == "num_indexes")) {
$arr_index = ss_snmp_uptime_get_indexes($hostname, $snmp_community, $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);
print sizeof($arr_index);
} elseif ($cmd == "query") {
$arg = $arg1;
$arr_index = ss_snmp_uptime_get_indexes($hostname, $snmp_community, $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);
$arr = ss_snmp_uptime_get_uptime($hostname, $snmp_community, $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);
for ($i=0;($i<sizeof($arr_index));$i++) {
if ($arg == "uptime") {
print $arr_index[$i] . "!" . $arr[$i] . "\n";
}elseif ($arg == "index") {
print $arr_index[$i] . "!" . $arr_index[$i] . "\n";
}
}
} elseif ($cmd == "get") {
$arg = $arg1;
$index = rtrim($arg2);
$arr_index = ss_snmp_uptime_get_indexes($hostname, $snmp_community, $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);
$arr = ss_snmp_uptime_get_uptime($hostname, $snmp_community, $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);
if (isset($arr_index[$index])) {
return $arr[$index];
} else {
return "ERROR: Invalid Return Value";
}
}
}
function ss_snmp_uptime_get_uptime($hostname, $snmp_community, $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) {
$arr = ss_snmp_uptime_reindex(cacti_snmp_walk($hostname, $snmp_community, ".1.3.6.1.2.1.1.3", $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));
$return_arr = array();
$j = 0;
for ($i=0;($i<sizeof($arr));$i++) {
if (is_numeric($arr[$i])) {
$return_arr[$j] = $arr[$i];
$j++;
}
}
return $return_arr;
}
function ss_snmp_uptime_get_indexes($hostname, $snmp_community, $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) {
$arr = ss_snmp_uptime_reindex(cacti_snmp_walk($hostname, $snmp_community, ".1.3.6.1.2.1.1.3", $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));
$return_arr = array();
$j = 0;
for ($i=0;($i<sizeof($arr));$i++) {
if (is_numeric($arr[$i])) {
$return_arr[$j] = $j;
$j++;
}
}
return $return_arr;
}
function ss_snmp_uptime_reindex($arr) {
$return_arr = array();
for ($i=0;($i<sizeof($arr));$i++) {
$return_arr[$i] = $arr[$i]["value"];
}
return $return_arr;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment