Location: PHPKode > projects > Network Managment / Inventory System > clearsite/include/aloe/cron/aloe_rrdwalk.php
#!/usr/bin/php
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2006-2008 Xinn.org                                        |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | ClearSite/ALOE, A Long Overdue Enterprise network managment solution!   |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Xinn.org. See     |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://aloeusers.net                                                    |
 | http://clearsite.sourceforge.net                                        |
 | http://aloenms.net                                                      |
 | http://clear-site.net                                                   |
 | http://clear-site.info                                                  |
 | http://xinn.org/ClearSite.html                                          |
 +-------------------------------------------------------------------------+
*/
$configpath = str_replace("/aloe/cron/aloe_rrdwalk.php", "", __FILE__) . "/config.php";
include_once($configpath);
include_once("$cs_base_path/include/aloe/aloe_config.php");
include_once("$base_path/lib/snmp.php");
include_once("$base_path/lib/rrd.php");
include_once("$base_path/../db_config.php");

snmp_set_quick_print(1);
snmp_set_oid_numeric_print(1);

// Grabs variables passed from aloe_cron.php

$device_ID        = $argv[1];

mysql_connect("$DBhost:$DBport",$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
$device_info_query = "SELECT t1.device_IP, t1.device_SNMP_Read, t1.device_type_ID, t2.template_table, t1.snmpver, t1.pingable FROM devices AS t1, snmp_template_list AS t2 WHERE t1.device_ID = \"$device_ID\" and t1.template_ID = t2.template_ID";
$device_info_results = mysql_query($device_info_query);
$device_info_row = mysql_fetch_assoc($device_info_results);
$device_IP        = $device_info_row["device_IP"];
$device_SNMP_Read = $device_info_row["device_SNMP_Read"];
$device_type_ID   = $device_info_row["device_type_ID"];
$template_table   = $device_info_row["template_table"];
$snmpver					= $device_info_row["snmpver"];
$pingable         = $device_info_row["pingable"];
$record_updated = date('Y-m-d H:i:s');
$error_log        = "";

if ($pingable == "Y") {
	php_ping($device_IP) or die("Device not responding to PING");
};

$crawl = array();

$template_sqlquery = "SELECT mib, oid, method, walk_type FROM $template_table WHERE walk_type = \"rrd\" or walk_type = \"both\" ORDER BY mib DESC";
$template_result = mysql_query($template_sqlquery)or die ("MySQL Error: " . mysql_error() . "\nQuery: " . $template_sqlquery . "\n\n");
while ($template_row = mysql_fetch_assoc($template_result)) {
	$crawl[$template_row["mib"]] = $template_row["walk_type"];
	if ($template_row["method"] == "walk") {
		${$template_row["mib"] . "_resarray"} = aloe_walk($device_IP, $device_SNMP_Read, $template_row["mib"], $template_row["oid"], $snmpver);
	} else {
		${$template_row["mib"] . "_resarray"}[] = snmpget($device_IP, $device_SNMP_Read, $template_row["oid"]);
	};
};

//Creating or updating RRDs

$opts_step = "--step"
					  ." 300";
$opts_rra_year = " RRA:AVERAGE:0.5:1:105408";
$opts_rra_month = " RRA:AVERAGE:0.5:1:8784";
		       

$descriptorspec = array(
	0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
	1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
	2 => array("pipe", "w")   // stderr is a file to write to
);
$fp = proc_open("$rrdtool_path -", $descriptorspec, $pipes);
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);

$traffic_create_output = "";
$traffic_create_error = "";
$traffic_update_output = "";
$traffic_update_error = "";

foreach ($cpu5min_resarray as $key => $value) {
	$cpu_filename = "$rrd_data_path/$device_IP.$key.cpu.rrd";
	if (!file_exists($cpu_filename)) {
		$opts_ds = " DS:cpu5min:GAUGE:600:0:100";
		$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
    $error_log .= aloe_rrd("create", $cpu_filename, $opts, $pipes);
  };
	$error_log .= aloe_rrd("update", $cpu_filename, " N:$cpu5min_resarray[$key]", $pipes);
	$sqlquery = "UPDATE cpus SET record_updated=\"$record_updated\" WHERE ifindex=\"$key\" and device_ID=\"$device_ID\"";
	$result = mysql_query($sqlquery);
};

if ($device_type_ID == "3" || $device_type_ID == "4" || $device_type_ID == "6" || $device_type_ID == "8" || $device_type_ID == "9") {
	foreach ($ifname_resarray as $key => $value) {
	  if ($device_type_ID != "9" or ($device_type_ID == "9" and array_search($value, $vlanids_resarray) === FALSE)) {
		  foreach ($crawl as $ckey => $cvalue) {
		  	if (empty(${$ckey . "_resarray"}[$key])) {
		  		${$ckey . "_resarray"}[$key] = "U";
		  	};
		  };
		  if ($ifoperstatus_resarray[$key] == "up") {
		  	$traffic_filename = "$rrd_data_path/$device_IP.$value.traffic.rrd";
		  	if (!file_exists($traffic_filename)) {
		  		$opts_ds =	  " DS:traffic_in:COUNTER:900:0:1000000000"
		  								 ." DS:traffic_out:COUNTER:900:0:1000000000";
		  		$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	      	$error_log .= aloe_rrd("create", $traffic_filename, $opts, $pipes);
	    	};
	    	if ($ifhcinoctets_resarray[$key] != "U" and !empty($ifhcinoctets_resarray[$key]) and $ifhcoutoctets_resarray[$key] != "U" and !empty($ifhcoutoctets_resarray[$key])) {
	    		$error_log .= aloe_rrd("update", $traffic_filename, " N:$ifhcinoctets_resarray[$key]:$ifhcoutoctets_resarray[$key]", $pipes);
	    	} else {
	    		$error_log .= aloe_rrd("update", $traffic_filename, " N:$ifinoctets_resarray[$key]:$ifoutoctets_resarray[$key]", $pipes);
	    	};
		  	$sqlquery = "UPDATE interfaces SET record_updated=\"$record_updated\" WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
		  	$result = mysql_query($sqlquery);
		  };
		};
	};
} else if ($device_type_ID == "1") {
	foreach($svname_resarray as $key => $value) {
		$sv_filename = "$rrd_data_path/$device_IP.$value.sv.rrd";
		if (!file_exists($sv_filename)) {
			$opts_ds = 		" DS:svavgld:GAUGE:600:0:255"
									 ." DS:svtcpcon:GAUGE:600:0:4294967295";
			$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	    $error_log .= aloe_rrd("create", $sv_filename, $opts, $pipes);
	  };
		$error_log .= aloe_rrd("update", $sv_filename, " N:$svavgld_resarray[$key]:$svtcpcon_resarray[$key]", $pipes);
		$sqlquery = "UPDATE css_sv SET record_updated=\"$record_updated\" WHERE sv_name=\"$value\" and device_ID=\"$device_ID\"";
		$result = mysql_query($sqlquery);
	};
	foreach($crname_resarray as $key => $value) {
		$cr_owner_query = "SELECT t2.owner_name FROM css_cr as t1, css_owner as t2 WHERE t1.cr_name = \"$value\" and t1.device_ID = \"$device_ID\" and t1.owner_ID = t2.owner_ID";
		$cr_owner_result = mysql_query($cr_owner_query);
		$cr_owner_row = mysql_fetch_assoc($cr_owner_result);
		$cr_filename = "$rrd_data_path/$device_IP.$cr_owner_row[owner_name].$value.cr.rrd";
		if (!file_exists($cr_filename)) {
			$opts_ds = 		" DS:crbytes:COUNTER:600:0:4294967295"
									 ." DS:crhits:COUNTER:600:0:4294967295";
			$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	    $error_log .= aloe_rrd("create", $cr_filename, $opts, $pipes);
	  };
		$error_log .= aloe_rrd("update", $cr_filename, " N:$crbytes_resarray[$key]:$crhits_resarray[$key]", $pipes);
		$sqlquery = "UPDATE css_cr SET record_updated=\"$record_updated\" WHERE cr_name=\"$value\" and device_ID=\"$device_ID\"";
		$result = mysql_query($sqlquery);
	};
	foreach($owner_resarray as $key => $value) {
		$own_filename = "$rrd_data_path/$device_IP.$value.own.rrd";
		if (!file_exists($own_filename)) {
			$opts_ds = 		" DS:ownerbytes:COUNTER:600:0:4294967295";
			$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	    $error_log .= aloe_rrd("create", $own_filename, $opts, $pipes);
	  };
		$error_log .= aloe_rrd("update", $own_filename, " N:$ownerbytes_resarray[$key]", $pipes);
		$sqlquery = "UPDATE css_owner SET record_updated=\"$record_updated\" WHERE owner_name=\"$value\" and device_ID=\"$device_ID\"";
		$result = mysql_query($sqlquery);
	};
	foreach ($ifdescr_resarray as $key => $value) {
		foreach ($crawl as $ckey => $cvalue) {
			if (empty(${$ckey . "_resarray"}[$key])) {
				${$ckey . "_resarray"}[$key] = "U";
			};
		};
		$traffic_filename = "$rrd_data_path/$device_IP.$value.traffic.rrd";
		if (!file_exists($traffic_filename)) {
			$opts_ds =	  " DS:traffic_in:COUNTER:600:0:1000000000"
									 ." DS:traffic_out:COUNTER:600:0:1000000000";
			$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	    $error_log .= aloe_rrd("create", $traffic_filename, $opts, $pipes);
	  };
	  $error_log .= aloe_rrd("update", $traffic_filename, " N:$ifhcinoctets_resarray[$key]:$ifhcoutoctets_resarray[$key]", $pipes);
	  $sqlquery = "UPDATE interfaces SET record_updated=\"$record_updated\" WHERE ifdescr=\"$value\" and device_ID=\"$device_ID\"";
		$result = mysql_query($sqlquery);
	};
} else if ($device_type_ID == "2" || $device_type_ID == "10") {
	foreach ($ifindex_resarray as $key => $value) {
		foreach ($crawl as $ckey => $cvalue) {
			if (empty(${$ckey . "_resarray"}[$key])) {
				${$ckey . "_resarray"}[$key] = "U";
			};
		};
		if ($ifoperstatus_resarray[$key] == "up") {
			$traffic_filename = "$rrd_data_path/$device_IP.$value.traffic.rrd";
			if (!file_exists($traffic_filename)) {
				$opts_ds =	  " DS:traffic_in:COUNTER:900:0:1000000000"
										 ." DS:traffic_out:COUNTER:900:0:1000000000";
				$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	    	$error_log .= aloe_rrd("create", $traffic_filename, $opts, $pipes);
	  	};
  		$error_log .= aloe_rrd("update", $traffic_filename, " N:$ifinoctets_resarray[$key]:$ifoutoctets_resarray[$key]", $pipes);
			$sqlquery = "UPDATE interfaces SET record_updated=\"$record_updated\" WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
			$result = mysql_query($sqlquery);
		};
	};
} else if ($device_type_ID =="7") {
	$stats_filename = "$rrd_data_path/$device_IP.stats.rrd";
	if (!file_exists($stats_filename)) {
		$opts_ds =	  " DS:active_clients:GAUGE:900:0:1000000000"
								 ." DS:ass_stats:COUNTER:900:0:1000000000";
		$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	 	$error_log .= aloe_rrd("create", $stats_filename, $opts, $pipes);
	};
	$error_log .= aloe_rrd("update", $stats_filename, " N:$active_clients_resarray[0]:$cDot11AssStatsAuthenticated_resarray[0]", $pipes);	
	foreach ($client_type_resarray as $key => $value) {
		if (!empty($client_name_resarray[$key]) and $client_name_resarray[$key] != "00") {
			$client_name = "";
			$split_client_name = split('-', $client_name_resarray[$key]);
			for ($x=0; $x<count($split_client_name); $x++) {
				$split_client_name[$x] = chr(hexdec($split_client_name[$x]));
				if (empty($client_name)) {
					$client_name = $split_client_name[$x];
				} else {
					$client_name .= "$split_client_name[$x]";
				};
			};
			$client_name = trim($client_name);
			$wap_client_filename = "$rrd_data_path/$client_name.wclient.rrd";
			if (!file_exists($wap_client_filename)) {
				$opts_ds = " DS:BytesReceived:DERIVE:900:0:54000000"
									." DS:BytesSent:DERIVE:900:0:54000000"
									." DS:SignalStrength:GAUGE:900:0:100"
									." DS:SigQuality:GAUGE:900:0:100";
				$opts = "$opts_step" . "$opts_ds" . "$opts_rra_month";
	    	$error_log .= aloe_rrd("create", $wap_client_filename, $opts, $pipes);
	  	};
	  	$cDot11ClientSignalStrength_resarray[$key] = $cDot11ClientSignalStrength_resarray[$key] + 100;
	  	$error_log .= aloe_rrd("update", $wap_client_filename, " N:$cDot11ClientBytesReceived_resarray[$key]:$cDot11ClientBytesSent_resarray[$key]:$cDot11ClientSignalStrength_resarray[$key]:$cDot11ClientSigQuality_resarray[$key]", $pipes);
			$sqlquery = "UPDATE wap_clients SET record_updated=\"$record_updated\" WHERE client_name=\"$client_name\" and device_ID=\"$device_ID\"";
			$result = mysql_query($sqlquery);
		};
	};
	foreach ($ifname_resarray as $key => $value) {
		foreach ($crawl as $ckey => $cvalue) {
			if (empty(${$ckey . "_resarray"}[$key])) {
				${$ckey . "_resarray"}[$key] = "U";
			};
		};
		if ($ifoperstatus_resarray[$key] == "up") {
			$traffic_filename = "$rrd_data_path/$device_IP.$value.traffic.rrd";
			if (!file_exists($traffic_filename)) {
				$opts_ds =	  " DS:traffic_in:COUNTER:900:0:1000000000"
										 ." DS:traffic_out:COUNTER:900:0:1000000000";
				$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	    	$error_log .= aloe_rrd("create", $traffic_filename, $opts, $pipes);
	  	};
  		$error_log .= aloe_rrd("update", $traffic_filename, " N:$ifinoctets_resarray[$key]:$ifoutoctets_resarray[$key]", $pipes);
			$sqlquery = "UPDATE interfaces SET record_updated=\"$record_updated\" WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
			$result = mysql_query($sqlquery);
		};
	};
} else if ($device_type_ID =="5") {
	$stats_filename = "$rrd_data_path/$device_IP.stats.rrd";
	if (!file_exists($stats_filename)) {
		$opts_ds =	  " DS:current_sessions:GAUGE:900:0:3000"
								 ." DS:lan_sessions:GAUGE:900:0:3000"
								 ." DS:max_sessions:GAUGE:900:0:3000";
		$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	 	$error_log .= aloe_rrd("create", $stats_filename, $opts, $pipes);
	};
	$error_log .= aloe_rrd("update", $stats_filename, " N:$current_sessions_resarray[0]:$lan_sessions_resarray[0]:$max_sessions_resarray[0]", $pipes);	
	foreach ($username_resarray as $key => $value) {
		$value = strtolower($value);
		$vpn_user_filename = "$rrd_data_path/$value.vpn.rrd";
		if (!file_exists($vpn_user_filename)) {
			$opts_ds = " DS:bytesrec:COUNTER:900:0:100000000"
								." DS:bytestrans:COUNTER:900:0:100000000";
			$opts = "$opts_step" . "$opts_ds" . "$opts_rra_month";
    	$error_log .= aloe_rrd("create", $vpn_user_filename, $opts, $pipes);
  	};
  	$error_log .= aloe_rrd("update", $vpn_user_filename, " N:$bytesrec_resarray[$key]:$bytestrans_resarray[$key]", $pipes);
		$sqlquery = "UPDATE vpn_sessions SET record_updated=\"$record_updated\" WHERE username=\"$value\" and device_ID=\"$device_ID\"";
		$result = mysql_query($sqlquery);
	};
	foreach ($ifindex_resarray as $key => $value) {
		foreach ($crawl as $ckey => $cvalue) {
			if (empty(${$ckey . "_resarray"}[$key])) {
				${$ckey . "_resarray"}[$key] = "U";
			};
		};
		if ($ifoperstatus_resarray[$key] == "up") {
			$traffic_filename = "$rrd_data_path/$device_IP.$value.traffic.rrd";
			if (!file_exists($traffic_filename)) {
				$opts_ds =	  " DS:traffic_in:COUNTER:900:0:1000000000"
										 ." DS:traffic_out:COUNTER:900:0:1000000000";
				$opts = "$opts_step" . "$opts_ds" . "$opts_rra_year";
	    	$error_log .= aloe_rrd("create", $traffic_filename, $opts, $pipes);
	  	};
  		$error_log .= aloe_rrd("update", $traffic_filename, " N:$ifinoctets_resarray[$key]:$ifoutoctets_resarray[$key]", $pipes);
			$sqlquery = "UPDATE interfaces SET record_updated=\"$record_updated\" WHERE ifname=\"$value\" and device_ID=\"$device_ID\"";
			$result = mysql_query($sqlquery);
		};
	};
};

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($fp);
print "rrd for $device_IP updated!\n";
if (!empty($error_log)) {
	print "Errors:\n$error_log\n\n";
} else {
	print "\n";
};
mysql_close();

?>
Return current item: Network Managment / Inventory System