function ping($host)
 {
     $pingObj = Net_Ping::factory();
     $response = $pingObj->ping($host);
     if (!isset($response->_transmitted)) {
         //we can't find the host name, assume it's off
         $response->_transmitted = 1;
         $response->_received = 0;
     }
     $result = array('transmitted' => $response->_transmitted, 'received' => $response->_received);
     return $result;
 }
 function ServiceStatus()
 {
     //require_once($_SERVER['DOCUMENT_ROOT'] . "/inc/classes/shared/ServerPing.class.php");
     //$this->ServerPing = new ServerPing();
     require "Net/Ping.php";
     //Net_Ping::setArgs($this->ServerPing);
     $this->ServerPing = Net_Ping::factory();
     /*$this->ServerPing = new Net_Ping();
     		
     		$this->ServerPing->setArgs($this->NetPingArguments);
     		
     		$this->ServerPing->factory();*/
 }
Beispiel #3
0
function ss_fping($hostname, $ping_sweeps = 6, $ping_type = "ICMP", $port = 80)
{
    /* record start time */
    list($micro, $seconds) = split(" ", microtime());
    $ss_fping_start = $seconds + $micro;
    $ping = new Net_Ping();
    $time = array();
    $total_time = 0;
    $failed_results = 0;
    $ping->host["hostname"] = gethostbyname($hostname);
    $ping->retries = 1;
    $ping->port = $port;
    $max = 0.0;
    $min = 9999.99;
    $dev = 0.0;
    $script_timeout = read_config_option("script_timeout");
    $ping_timeout = read_config_option("ping_timeout");
    switch ($ping_type) {
        case "ICMP":
            $method = PING_ICMP;
            break;
        case "TCP":
            $method = PING_TCP;
            break;
        case "UDP":
            $method = PING_UDP;
            break;
    }
    $i = 0;
    while ($i < $ping_sweeps) {
        $result = $ping->ping(AVAIL_PING, $method, read_config_option("ping_timeout"), 1);
        if (!$result) {
            $failed_results++;
        } else {
            $time[$i] = $ping->ping_status;
            $total_time += $ping->ping_status;
            if ($ping->ping_status < $min) {
                $min = $ping->ping_status;
            }
            if ($ping->ping_status > $max) {
                $max = $ping->ping_status;
            }
        }
        $i++;
        /* get current time */
        list($micro, $seconds) = split(" ", microtime());
        $ss_fping_current = $seconds + $micro;
        /* if called from script server, end one second before a timeout occurs */
        if (isset($called_by_script_server) && $ss_fping_current - $ss_fping_start + $ping_timeout / 1000 + 1 > $script_timeout) {
            $ping_sweeps = $i;
            break;
        }
    }
    if ($failed_results == $ping_sweeps) {
        return "loss:100.00";
    } else {
        $loss = $failed_results / $ping_sweeps * 100;
        $avg = $total_time / ($ping_sweeps - $failed_results);
        /* calculate standard deviation */
        $predev = 0;
        foreach ($time as $sample) {
            $predev += pow($sample - $avg, 2);
        }
        $dev = sqrt($predev / count($time));
        return sprintf("min:%0.4f avg:%0.4f max:%0.4f dev:%0.4f loss:%0.4f", $min, $avg, $max, $dev, $loss);
    }
}
Beispiel #4
0
function ping_host()
{
    input_validate_input_number($_REQUEST['id']);
    if (empty($_REQUEST['id'])) {
        return "";
    }
    $host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array($_REQUEST['id']));
    $am = $host['availability_method'];
    $anym = false;
    if ($am == AVAIL_SNMP || $am == AVAIL_SNMP_GET_NEXT || $am == AVAIL_SNMP_GET_SYSDESC || $am == AVAIL_SNMP_AND_PING || $am == AVAIL_SNMP_OR_PING) {
        $anym = true;
        print "SNMP Information<br>\n";
        print "<span style='font-size: 10px; font-weight: normal; font-family: monospace;'>\n";
        if ($host['snmp_community'] == '' && $host['snmp_username'] == '' || $host['snmp_version'] == 0) {
            print "<span style='color: #ab3f1e; font-weight: bold;'>SNMP not in use</span>\n";
        } else {
            $snmp_system = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.1.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
            /* modify for some system descriptions */
            /* 0000937: System output in host.php poor for Alcatel */
            if (substr_count($snmp_system, '00:')) {
                $snmp_system = str_replace('00:', '', $snmp_system);
                $snmp_system = str_replace(':', ' ', $snmp_system);
            }
            if ($snmp_system == '') {
                print "<span class='hostDown'>SNMP error</span>\n";
            } else {
                $snmp_uptime = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.3.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
                $snmp_hostname = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.5.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
                $snmp_location = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.6.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
                $snmp_contact = cacti_snmp_get($host['hostname'], $host['snmp_community'], '.1.3.6.1.2.1.1.4.0', $host['snmp_version'], $host['snmp_username'], $host['snmp_password'], $host['snmp_auth_protocol'], $host['snmp_priv_passphrase'], $host['snmp_priv_protocol'], $host['snmp_context'], $host['snmp_port'], $host['snmp_timeout'], read_config_option('snmp_retries'), SNMP_WEBUI);
                print '<strong>System:</strong>' . html_split_string($snmp_system) . "<br>\n";
                $days = intval($snmp_uptime / (60 * 60 * 24 * 100));
                $remainder = $snmp_uptime % (60 * 60 * 24 * 100);
                $hours = intval($remainder / (60 * 60 * 100));
                $remainder = $remainder % (60 * 60 * 100);
                $minutes = intval($remainder / (60 * 100));
                print "<strong>Uptime:</strong> {$snmp_uptime}";
                print "&nbsp;({$days} days, {$hours} hours, {$minutes} minutes)<br>\n";
                print "<strong>Hostname:</strong> {$snmp_hostname}<br>\n";
                print "<strong>Location:</strong> {$snmp_location}<br>\n";
                print "<strong>Contact:</strong> {$snmp_contact}<br>\n";
            }
        }
        print "</span>\n";
    }
    if ($am == AVAIL_PING || $am == AVAIL_SNMP_AND_PING || $am == AVAIL_SNMP_OR_PING) {
        $anym = true;
        /* create new ping socket for host pinging */
        $ping = new Net_Ping();
        $ping->host = $host;
        $ping->port = $host['ping_port'];
        /* perform the appropriate ping check of the host */
        $ping_results = $ping->ping(AVAIL_PING, $host['ping_method'], $host['ping_timeout'], $host['ping_retries']);
        if ($ping_results == true) {
            $host_down = false;
            $class = 'hostUp';
        } else {
            $host_down = true;
            $class = 'hostDown';
        }
        print "Ping Results<br>\n";
        print "<span class='" . $class . "'>" . $ping->ping_response . "</span>\n";
    }
    if ($anym == false) {
        print "No Ping or SNMP Availability Check In Use<br><br>\n";
    }
}
Beispiel #5
0
				<td>History</td>
			</tr>
		</thead>
		<tbody>
		
	<?php 
$sql = "Select pkCabinetDetailsID, pkCabinetID, ServerLabel, IP, Client, pkClientID, SendEmail, SwitchPort, SwitchLabel, PDULabel, PDUPort, Password from tblCabinetDetails\n\tleft outer join tblClient on pkClientID = fkClientID\n\tleft outer join tblCabinet on pkCabinetID = fkCabinetID\n\t\twhere IP > ''\n\torder by pkCabinetID, SwitchPort, ServerLabel";
$sqld = "delete from tblPing\n\t\t\twhere \n\t\t\tdate_format(date_sub(concat(CURRENT_DATE(), ' ', CURRENT_TIME()), INTERVAL 24 hour),'%Y-%m-%d %h:%i:%s') \n\t\t\t>= \n\t\t\tdate_format(datetime,'%Y-%m-%d %h:%i:%s')\n\t\t\t";
$resd = $db->executeNonQuery($sqld);
//execute the query
$res = $db->executeQuery($sql);
//execute the query
$i = 0;
$class = ' altrow';
while ($res != -1 && ($row = mysql_fetch_assoc($res))) {
    $ping = Net_Ping::factory();
    if (PEAR::isError($ping)) {
        echo $ping->getMessage();
    } else {
        $ping->setArgs(array('count' => 1));
        $results = object_2_array($ping->ping($row['IP']));
        $r = 0;
        while ($results['_received'] == 0 && $r < 5) {
            $results = object_2_array($ping->ping($row['IP']));
            $r++;
        }
    }
    //print_r($results);
    $htmlx = "Login: "******", Password: "******"\n";
    $sqlp = "Select * from tblCabinetDetails where fkCabinetID in (\n\tSelect fkCabinetID from tblCabinetDetails where IP = '" . $row['IP'] . "'\n\t) and fkClientID in (10,18)";
    $resp = $db->executeQuery($sqlp);
Beispiel #6
0
function host_edit()
{
    global $colors, $fields_host_edit, $reindex_types;
    /* ================= input validation ================= */
    input_validate_input_number(get_request_var("id"));
    /* ==================================================== */
    display_output_messages();
    if (!empty($_GET["id"])) {
        $host = db_fetch_row("select * from host where id=" . $_GET["id"]);
        $header_label = "[edit: " . $host["description"] . "]";
    } else {
        $header_label = "[new]";
    }
    if (!empty($host["id"])) {
        ?>
		<table width="100%" align="center">
			<tr>
				<td class="textInfo" colspan="2">
					<?php 
        print $host["description"];
        ?>
 (<?php 
        print $host["hostname"];
        ?>
)
				</td>
			</tr>
			<tr>
				<td class="textHeader">
				<?php 
        if ($host["availability_method"] == AVAIL_SNMP || $host["availability_method"] == AVAIL_SNMP_AND_PING || $host["availability_method"] == AVAIL_SNMP_OR_PING) {
            ?>
					SNMP Information<br>

					<span style="font-size: 10px; font-weight: normal; font-family: monospace;">
					<?php 
            if ($host["snmp_community"] == "" && $host["snmp_username"] == "" || $host["snmp_version"] == 0) {
                print "<span style='color: #ab3f1e; font-weight: bold;'>SNMP not in use</span>\n";
            } else {
                $snmp_system = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.1.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);
                /* modify for some system descriptions */
                /* 0000937: System output in hosts.php poor for Alcatel */
                if (substr_count($snmp_system, "00:")) {
                    $snmp_system = str_replace("00:", "", $snmp_system);
                    $snmp_system = str_replace(":", " ", $snmp_system);
                }
                if ($snmp_system == "") {
                    print "<span style='color: #ff0000; font-weight: bold;'>SNMP error</span>\n";
                } else {
                    $snmp_uptime = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.3.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);
                    $snmp_hostname = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.5.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);
                    $snmp_location = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.6.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);
                    $snmp_contact = cacti_snmp_get($host["hostname"], $host["snmp_community"], ".1.3.6.1.2.1.1.4.0", $host["snmp_version"], $host["snmp_username"], $host["snmp_password"], $host["snmp_auth_protocol"], $host["snmp_priv_passphrase"], $host["snmp_priv_protocol"], $host["snmp_context"], $host["snmp_port"], $host["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);
                    print "<strong>System:</strong>" . html_split_string($snmp_system) . "<br>\n";
                    $days = intval($snmp_uptime / (60 * 60 * 24 * 100));
                    $remainder = $snmp_uptime % (60 * 60 * 24 * 100);
                    $hours = intval($remainder / (60 * 60 * 100));
                    $remainder = $remainder % (60 * 60 * 100);
                    $minutes = intval($remainder / (60 * 100));
                    print "<strong>Uptime:</strong> {$snmp_uptime}";
                    print "&nbsp;({$days} days, {$hours} hours, {$minutes} minutes)<br>\n";
                    print "<strong>Hostname:</strong> {$snmp_hostname}<br>\n";
                    print "<strong>Location:</strong> {$snmp_location}<br>\n";
                    print "<strong>Contact:</strong> {$snmp_contact}<br>\n";
                }
            }
            ?>
					</span>
				<?php 
        }
        if ($host["availability_method"] == AVAIL_PING || $host["availability_method"] == AVAIL_SNMP_AND_PING || $host["availability_method"] == AVAIL_SNMP_OR_PING) {
            /* create new ping socket for host pinging */
            $ping = new Net_Ping();
            $ping->host = $host;
            $ping->port = $host["ping_port"];
            /* perform the appropriate ping check of the host */
            if ($ping->ping($host["availability_method"], $host["ping_method"], $host["ping_timeout"], $host["ping_retries"])) {
                $host_down = false;
                $color = "#000000";
            } else {
                $host_down = true;
                $color = "#ff0000";
            }
            ?>
					<br>Ping Results<br>
					<span style="font-size: 10px; font-weight: normal; color: <?php 
            print $color;
            ?>
; font-family: monospace;">
					<?php 
            print $ping->ping_response;
            ?>
					</span>
				<?php 
        } else {
            if ($host["availability_method"] == AVAIL_NONE) {
                ?>
					No Availability Check In Use<br>
				<?php 
            }
        }
        ?>
				</td>
				<td class="textInfo" valign="top">
					<span style="color: #c16921;">*</span><a href="graphs_new.php?host_id=<?php 
        print $host["id"];
        ?>
">Create Graphs for this Host</a><br>
					<span style="color: #c16921;">*</span><a href="data_sources.php?host_id=<?php 
        print $host["id"];
        ?>
&ds_rows=30&filter=&template_id=-1&method_id=-1&page=1">Data Source List</a><br>
					<span style="color: #c16921;">*</span><a href="graphs.php?host_id=<?php 
        print $host["id"];
        ?>
&graph_rows=30&filter=&template_id=-1&page=1">Graph List</a>
				</td>
			</tr>
		</table>
		<?php 
    }
    html_start_box("<strong>Devices</strong> {$header_label}", "100%", $colors["header"], "3", "center", "");
    /* preserve the host template id if passed in via a GET variable */
    if (!empty($_GET["host_template_id"])) {
        $fields_host_edit["host_template_id"]["value"] = $_GET["host_template_id"];
    }
    draw_edit_form(array("config" => array("form_name" => "chk"), "fields" => inject_form_variables($fields_host_edit, isset($host) ? $host : array())));
    html_end_box();
    ?>
	<script type="text/javascript">
	<!--

	// default snmp information
	var snmp_community       = document.getElementById('snmp_community').value;
	var snmp_username        = document.getElementById('snmp_username').value;
	var snmp_password        = document.getElementById('snmp_password').value;
	var snmp_auth_protocol   = document.getElementById('snmp_auth_protocol').value;
	var snmp_priv_passphrase = document.getElementById('snmp_priv_passphrase').value;
	var snmp_priv_protocol   = document.getElementById('snmp_priv_protocol').value;
	var snmp_context         = document.getElementById('snmp_context').value;
	var snmp_port            = document.getElementById('snmp_port').value;
	var snmp_timeout         = document.getElementById('snmp_timeout').value;
	var max_oids             = document.getElementById('max_oids').value;

	// default ping methods
	var ping_method    = document.getElementById('ping_method').value;
	var ping_port      = document.getElementById('ping_port').value;
	var ping_timeout   = document.getElementById('ping_timeout').value;
	var ping_retries   = document.getElementById('ping_retries').value;

	var availability_methods = document.getElementById('availability_method').options;
	var num_methods          = document.getElementById('availability_method').length;
	var selectedIndex        = document.getElementById('availability_method').selectedIndex;

	var agent = navigator.userAgent;
	agent = agent.match("MSIE");

	function setPingVisibility() {
		availability_method = document.getElementById('availability_method').value;
		ping_method         = document.getElementById('ping_method').value;

		/* debugging, uncomment as required */
		//alert("The availability method is '" + availability_method + "'");
		//alert("The ping method is '" + ping_method + "'");

		switch(availability_method) {
		case "0": // none
			document.getElementById('row_ping_method').style.display  = "none";
			document.getElementById('row_ping_port').style.display    = "none";
			document.getElementById('row_ping_timeout').style.display = "none";
			document.getElementById('row_ping_retries').style.display = "none";

			break;
		case "2": // snmp
			document.getElementById('row_ping_method').style.display  = "none";
			document.getElementById('row_ping_port').style.display    = "none";
			document.getElementById('row_ping_timeout').style.display = "";
			document.getElementById('row_ping_retries').style.display = "";

			break;
		default: // ping ok
			switch(ping_method) {
			case "1": // ping icmp
				document.getElementById('row_ping_method').style.display  = "";
				document.getElementById('row_ping_port').style.display    = "none";
				document.getElementById('row_ping_timeout').style.display = "";
				document.getElementById('row_ping_retries').style.display = "";

				break;
			case "2": // ping udp
			case "3": // ping tcp
				document.getElementById('row_ping_method').style.display  = "";
				document.getElementById('row_ping_port').style.display    = "";
				document.getElementById('row_ping_timeout').style.display = "";
				document.getElementById('row_ping_retries').style.display = "";

				break;
			}

			break;
		}
	}

	function addSelectItem(item, formObj) {
		if (agent != "MSIE") {
			formObj.add(item,null); // standards compliant
		}else{
			formObj.add(item);      // IE only
		}
	}

	function setAvailability(type) {
		/* get the availability structure */
		var am=document.getElementById('availability_method');

		/* get current selectedIndex */
		selectedIndex = document.getElementById('availability_method').selectedIndex;

		/* debugging uncomment as required */
		//alert("The selectedIndex is '" + selectedIndex + "'");
		//alert("The array length is '" + am.length + "'");

		switch(type) {
		case "NoSNMP":
			/* remove snmp options */
			if (am.length == 5) {
				am.remove(1);
				am.remove(1);
				am.remove(1);
			}

			/* set the index to something valid, like "ping" */
			if (selectedIndex > 1) {
				am.selectedIndex=1;
			}

			break;
		case "All":
			/* restore all options */
			if (am.length == 2) {
				am.remove(0);
				am.remove(0);

				var a=document.createElement('option');
				var b=document.createElement('option');
				var c=document.createElement('option');
				var d=document.createElement('option');
				var e=document.createElement('option');

				a.value="0";
				a.text="None";
				addSelectItem(a,am);

				b.value="1";
				b.text="Ping and SNMP";
				addSelectItem(b,am);

				e.value="4";
				e.text="Ping or SNMP";
				addSelectItem(e,am);

				c.value="2";
				c.text="SNMP";
				addSelectItem(c,am);

				d.value="3";
				d.text="Ping";
				addSelectItem(d,am);

				/* restore the correct index number */
				if (selectedIndex == 0) {
					am.selectedIndex = 0;
				}else{
					am.selectedIndex = 3;
				}
			}

			break;
		}

		setAvailabilityVisibility(type, am.selectedIndex);
		setPingVisibility();
	}

	function setAvailabilityVisibility(type, selectedIndex) {
		switch(type) {
		case "NoSNMP":
			switch(selectedIndex) {
			case "0": // availability none
				document.getElementById('row_ping_method').style.display="none";
				document.getElementById('ping_method').value=0;

				break;
			case "1": // ping
				document.getElementById('row_ping_method').style.display="";
				document.getElementById('ping_method').value=ping_method;

				break;
			}
		case "All":
			switch(selectedIndex) {
			case "0": // availability none
				document.getElementById('row_ping_method').style.display="none";
				document.getElementById('ping_method').value=0;

				break;
			case "1": // ping and snmp
			case "3": // ping
			case "4": // ping or snmp
				if ((document.getElementById('row_ping_method').style.display == "none") ||
					(document.getElementById('row_ping_method').style.display == undefined)) {
					document.getElementById('ping_method').value=ping_method;
					document.getElementById('row_ping_method').style.display="";
				}

				break;
			case "2": // snmp
				document.getElementById('row_ping_method').style.display="none";
				document.getElementById('ping_method').value="0";

				break;
			}
		}
	}

	function changeHostForm() {
		snmp_version        = document.getElementById('snmp_version').value;

		switch(snmp_version) {
		case "0":
			setAvailability("NoSNMP");
			setSNMP("None");

			break;
		case "1":
		case "2":
			setAvailability("All");
			setSNMP("v1v2");

			break;
		case "3":
			setAvailability("All");
			setSNMP("v3");

			break;
		}
	}

	function setSNMP(snmp_type) {
		switch(snmp_type) {
		case "None":
			document.getElementById('row_snmp_username').style.display        = "none";
			document.getElementById('row_snmp_password').style.display        = "none";
			document.getElementById('row_snmp_community').style.display       = "none";
			document.getElementById('row_snmp_auth_protocol').style.display   = "none";
			document.getElementById('row_snmp_priv_passphrase').style.display = "none";
			document.getElementById('row_snmp_priv_protocol').style.display   = "none";
			document.getElementById('row_snmp_context').style.display         = "none";
			document.getElementById('row_snmp_port').style.display            = "none";
			document.getElementById('row_snmp_timeout').style.display         = "none";
			document.getElementById('row_max_oids').style.display             = "none";

			break;
		case "v1v2":
			document.getElementById('row_snmp_username').style.display        = "none";
			document.getElementById('row_snmp_password').style.display        = "none";
			document.getElementById('row_snmp_community').style.display       = "";
			document.getElementById('row_snmp_auth_protocol').style.display   = "none";
			document.getElementById('row_snmp_priv_passphrase').style.display = "none";
			document.getElementById('row_snmp_priv_protocol').style.display   = "none";
			document.getElementById('row_snmp_context').style.display         = "none";
			document.getElementById('row_snmp_port').style.display            = "";
			document.getElementById('row_snmp_timeout').style.display         = "";
			document.getElementById('row_max_oids').style.display             = "";

			break;
		case "v3":
			document.getElementById('row_snmp_username').style.display        = "";
			document.getElementById('row_snmp_password').style.display        = "";
			document.getElementById('row_snmp_community').style.display       = "none";
			document.getElementById('row_snmp_auth_protocol').style.display   = "";
			document.getElementById('row_snmp_priv_passphrase').style.display = "";
			document.getElementById('row_snmp_priv_protocol').style.display   = "";
			document.getElementById('row_snmp_context').style.display         = "";
			document.getElementById('row_snmp_port').style.display            = "";
			document.getElementById('row_snmp_timeout').style.display         = "";
			document.getElementById('row_max_oids').style.display             = "";

			break;
		}
	}

	function addLoadEvent(func) {
		var oldonload = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = func;
		} else {
			window.onload = function() {
				if (oldonload) {
					oldonload();
				}
				func();
			}
		}
	}

	addLoadEvent(changeHostForm);

	-->
	</script>
	<?php 
    if (isset($_GET["display_dq_details"]) && isset($_SESSION["debug_log"]["data_query"])) {
        html_start_box("<strong>Data Query Debug Information</strong>", "100%", $colors["header"], "3", "center", "");
        print "<tr><td><span style='font-family: monospace;'>" . debug_log_return("data_query") . "</span></td></tr>";
        html_end_box();
    }
    if (!empty($host["id"])) {
        html_start_box("<strong>Associated Graph Templates</strong>", "100%", $colors["header"], "3", "center", "");
        html_header(array("Graph Template Name", "Status"), 2);
        $selected_graph_templates = db_fetch_assoc("select\n\t\t\tgraph_templates.id,\n\t\t\tgraph_templates.name\n\t\t\tfrom (graph_templates,host_graph)\n\t\t\twhere graph_templates.id=host_graph.graph_template_id\n\t\t\tand host_graph.host_id=" . $_GET["id"] . "\n\t\t\torder by graph_templates.name");
        $available_graph_templates = db_fetch_assoc("SELECT\n\t\t\tgraph_templates.id, graph_templates.name\n\t\t\tFROM snmp_query_graph RIGHT JOIN graph_templates\n\t\t\tON (snmp_query_graph.graph_template_id = graph_templates.id)\n\t\t\tWHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name");
        $i = 0;
        if (sizeof($selected_graph_templates) > 0) {
            foreach ($selected_graph_templates as $item) {
                $i++;
                /* get status information for this graph template */
                $is_being_graphed = sizeof(db_fetch_assoc("select id from graph_local where graph_template_id=" . $item["id"] . " and host_id=" . $_GET["id"])) > 0 ? true : false;
                ?>
			<tr>
				<td style="padding: 4px;">
					<strong><?php 
                print $i;
                ?>
)</strong> <?php 
                print $item["name"];
                ?>
				</td>
				<td>
					<?php 
                print $is_being_graphed == true ? "<span style='color: green;'>Is Being Graphed</span> (<a href='graphs.php?action=graph_edit&id=" . db_fetch_cell("select id from graph_local where graph_template_id=" . $item["id"] . " and host_id=" . $_GET["id"] . " limit 0,1") . "'>Edit</a>)" : "<span style='color: #484848;'>Not Being Graphed</span>";
                ?>
				</td>
				<td align='right' nowrap>
					<a href='host.php?action=gt_remove&id=<?php 
                print $item["id"];
                ?>
&host_id=<?php 
                print $_GET["id"];
                ?>
'><img src='images/delete_icon_large.gif' title='Delete Graph Template Association' alt='Delete Graph Template Association' border='0' align='absmiddle'></a>
				</td>
			</tr>
			<?php 
            }
        } else {
            print "<tr><td><em>No associated graph templates.</em></td></tr>";
        }
        ?>
		<tr bgcolor="#<?php 
        print $colors["form_alternate1"];
        ?>
">
			<td colspan="4">
				<table cellspacing="0" cellpadding="1" width="100%">
					<td nowrap>Add Graph Template:&nbsp;
						<?php 
        form_dropdown("graph_template_id", $available_graph_templates, "name", "id", "", "", "");
        ?>
					</td>
					<td align="right">
						&nbsp;<input type="image" src="images/button_add.gif" alt="Add" name="add_gt" align="absmiddle">
					</td>
				</table>
			</td>
		</tr>

		<?php 
        html_end_box();
        html_start_box("<strong>Associated Data Queries</strong>", "100%", $colors["header"], "3", "center", "");
        html_header(array("Data Query Name", "Debugging", "Re-Index Method", "Status"), 2);
        $selected_data_queries = db_fetch_assoc("select\n\t\t\tsnmp_query.id,\n\t\t\tsnmp_query.name,\n\t\t\thost_snmp_query.reindex_method\n\t\t\tfrom (snmp_query,host_snmp_query)\n\t\t\twhere snmp_query.id=host_snmp_query.snmp_query_id\n\t\t\tand host_snmp_query.host_id=" . $_GET["id"] . "\n\t\t\torder by snmp_query.name");
        $available_data_queries = db_fetch_assoc("select\n\t\t\tsnmp_query.id,\n\t\t\tsnmp_query.name\n\t\t\tfrom snmp_query\n\t\t\torder by snmp_query.name");
        $keeper = array();
        foreach ($available_data_queries as $item) {
            if (sizeof(db_fetch_assoc("SELECT snmp_query_id FROM host_snmp_query " . " WHERE ((host_id=" . $_GET["id"] . ")" . " and (snmp_query_id=" . $item["id"] . "))")) > 0) {
                /* do nothing */
            } else {
                array_push($keeper, $item);
            }
        }
        $available_data_queries = $keeper;
        $i = 0;
        if (sizeof($selected_data_queries) > 0) {
            foreach ($selected_data_queries as $item) {
                $i++;
                /* get status information for this data query */
                $num_dq_items = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"]));
                $num_dq_rows = sizeof(db_fetch_assoc("select snmp_index from host_snmp_cache where host_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"] . " group by snmp_index"));
                $status = "success";
                ?>
			<tr>
				<td style="padding: 4px;">
					<strong><?php 
                print $i;
                ?>
)</strong> <?php 
                print $item["name"];
                ?>
				</td>
				<td>
					(<a href="host.php?action=query_verbose&id=<?php 
                print $item["id"];
                ?>
&host_id=<?php 
                print $_GET["id"];
                ?>
">Verbose Query</a>)
				</td>
				<td>
					<?php 
                print $reindex_types[$item["reindex_method"]];
                ?>
				</td>
				<td>
					<?php 
                print $status == "success" ? "<span style='color: green;'>Success</span>" : "<span style='color: green;'>Fail</span>";
                ?>
 [<?php 
                print $num_dq_items;
                ?>
 Item<?php 
                print $num_dq_items == 1 ? "" : "s";
                ?>
, <?php 
                print $num_dq_rows;
                ?>
 Row<?php 
                print $num_dq_rows == 1 ? "" : "s";
                ?>
]
				</td>
				<td align='right' nowrap>
					<a href='host.php?action=query_reload&id=<?php 
                print $item["id"];
                ?>
&host_id=<?php 
                print $_GET["id"];
                ?>
'><img src='images/reload_icon_small.gif' title='Reload Data Query' alt='Reload Data Query' border='0' align='absmiddle'></a>&nbsp;
					<a href='host.php?action=query_remove&id=<?php 
                print $item["id"];
                ?>
&host_id=<?php 
                print $_GET["id"];
                ?>
'><img src='images/delete_icon_large.gif' title='Delete Data Query Association' alt='Delete Data Query Association' border='0' align='absmiddle'></a>
				</td>
			</tr>
			<?php 
            }
        } else {
            print "<tr><td><em>No associated data queries.</em></td></tr>";
        }
        ?>
		<tr bgcolor="#<?php 
        print $colors["form_alternate1"];
        ?>
">
			<td colspan="5">
				<table cellspacing="0" cellpadding="1" width="100%">
					<td nowrap>Add Data Query:&nbsp;
						<?php 
        form_dropdown("snmp_query_id", $available_data_queries, "name", "id", "", "", "");
        ?>
					</td>
					<td nowrap>Re-Index Method:&nbsp;
						<?php 
        form_dropdown("reindex_method", $reindex_types, "", "", read_config_option("reindex_method"), "", "");
        ?>
					</td>
					<td align="right">
						&nbsp;<input type="image" src="images/button_add.gif" alt="Add" name="add_dq" align="absmiddle">
					</td>
				</table>
			</td>
		</tr>

		<?php 
        html_end_box();
    }
    form_save_button("host.php");
}
Beispiel #7
0
        }
    } else {
        cacti_log("ERROR: Invalid Number of Arguments.  You must specify 0 or 2 arguments.", $print_data_to_stdout);
        /* record the process as having completed */
        record_cmdphp_done();
        exit("-1");
    }
}
if (sizeof($polling_items) > 0 && read_config_option("poller_enabled") == "on") {
    $failure_type = "";
    $host_down = false;
    $new_host = true;
    $last_host = "";
    $current_host = "";
    /* create new ping socket for host pinging */
    $ping = new Net_Ping();
    /* startup Cacti php polling server and include the include file for script processing */
    if ($script_server_calls > 0) {
        $cactides = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
        if (function_exists("proc_open")) {
            $cactiphp = proc_open(read_config_option("path_php_binary") . " -q " . $config["base_path"] . "/script_server.php cmd", $cactides, $pipes);
            $output = fgets($pipes[1], 1024);
            if (substr_count($output, "Started") != 0) {
                if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_HIGH) {
                    cacti_log("PHP Script Server Started Properly", $print_data_to_stdout);
                }
            }
            $using_proc_function = true;
        } else {
            $using_proc_function = false;
            if (read_config_option("log_verbosity") == POLLER_VERBOSITY_DEBUG) {
Beispiel #8
0
 /**
  * Factory for Net_Ping
  *
  * @access public
  */
 function factory()
 {
     $ping_path = '';
     $sysname = Net_Ping::_setSystemName();
     if (($ping_path = Net_Ping::_setPingPath($sysname)) == NET_PING_CANT_LOCATE_PING_BINARY) {
         return PEAR::throwError(NET_PING_CANT_LOCATE_PING_BINARY_MSG, NET_PING_CANT_LOCATE_PING_BINARY);
     } else {
         return new Net_Ping($ping_path, $sysname);
     }
 }
Beispiel #9
0
function ss_fping($hostname, $ping_sweeps=6, $ping_type="ICMP", $port=80) {
	$ping = new Net_Ping;

	$time = array();
	$total_time = 0;
	$failed_results = 0;

	$ping->host["hostname"] = $hostname;
	$ping->retries = 1;
	$ping->port = $port;
	$max = 0.0;
	$min = 9999.99;
	$dev = 0.0;

	switch ($ping_type) {
	case "ICMP":
		$method = PING_ICMP;
		break;
	case "TCP":
		$method = PING_TCP;
		break;
	case "UDP":
		$method = PING_UDP;
		break;
	}

	$i = 0;
	while ($i < $ping_sweeps) {
		$result = $ping->ping(AVAIL_PING,
					$method,
					read_config_option("ping_timeout"),
					1);

		if (!$result) {
			$failed_results++;
		}else{
			$time[$i] = $ping->ping_status;
			$total_time += $ping->ping_status;
			if ($ping->ping_status < $min) $min = $ping->ping_status;
			if ($ping->ping_status > $max) $max = $ping->ping_status;
		}

		$i++;
	}

	if ($failed_results == $ping_sweeps) {
		return "loss:100.00";
	}else{
		$loss = ($failed_results/$ping_sweeps) * 100;
	    $avg = $total_time/($ping_sweeps-$failed_results);

		/* calculate standard deviation */
		$predev = 0;
		foreach($time as $sample) {
			$predev += pow(($sample-$avg),2);
		}
		$dev = sqrt($predev / count($time));

		return sprintf("min:%0.4f avg:%0.4f max:%0.4f dev:%0.4f loss:%0.4f", $min, $avg, $max, $dev, $loss);
	}
}
Beispiel #10
0
 /**
  * Ping selected address with PEAR ping package
  *
  * @access protected
  * @param ip $address
  * @return void
  */
 protected function ping_address_method_pear($address)
 {
     # we need pear ping package
     require_once dirname(__FILE__) . '/../../functions/PEAR/Net/Ping.php';
     $ping = Net_Ping::factory();
     # check for errors
     if ($ping->pear->isError($ping)) {
         $this->throw_exception("Error: " . $ping->getMessage());
     } else {
         //set count and timeout
         $ping->setArgs(array('count' => $this->icmp_timeout, 'timeout' => $this->icmp_timeout));
         //execute
         $ping_response = $ping->ping($address);
         //check response for error
         if ($ping->pear->isError($ping_response)) {
             $result['code'] = 2;
         } else {
             //all good
             if ($ping_response->_transmitted == $ping_response->_received) {
                 $result['code'] = 0;
                 $this->rtt = "RTT: " . strstr($ping_response->_round_trip['avg'], ".", true);
             } elseif ($ping_response->_received == 0) {
                 $result['code'] = 1;
             } else {
                 $result['code'] = 3;
             }
         }
     }
     //return result for web or cmd
     exit($result['code']);
 }
Beispiel #11
0
				AND device_id>=$first
				AND device_id<=$last" .
				($poller_id == 0 ? "" : " AND poller_id=$poller_id"));
	}
}

if ((sizeof($polling_items) > 0) && (read_config_option("poller_enabled") == CHECKED)) {
	$failure_type = "";
	$device_down    = false;
	$new_device     = true;
	$last_device    = "";
	$current_device = "";
	$poll_time    = 0;

	/* create new ping socket for device pinging */
	$ping = new Net_Ping;

	/* startup Cacti php polling server and include the include file for script processing */
	if ($script_server_calls > 0) {
		$cactides = 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 pipe to write to
			);

		if (function_exists("proc_open")) {
			$cactiphp = proc_open(read_config_option("path_php_binary") . " -q " . CACTI_BASE_PATH . "/script_server.php cmd", $cactides, $pipes);
			$output = fgets($pipes[1], 1024);
			if (substr_count($output, "Started") != 0) {
				if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_HIGH) {
					cacti_log("PHP Script Server Started Properly",$print_data_to_stdout);
Beispiel #12
0
/**
 * Ping host - PEAR
 */
function pingHostPear($ip, $count = "1", $timeout = 1, $exit = false)
{
    require_once "PEAR/Net/Ping.php";
    $ping = Net_Ping::factory();
    if (PEAR::isError($ping)) {
        echo $ping->getMessage();
    } else {
        $ping->setArgs(array('count' => $count, 'timeout' => 1));
        $pRes = $ping->ping($ip);
        // check response
        if (PEAR::isError($pRes)) {
            $result['code'] = 2;
            $result['text'] = $pRes->message;
            $result['text'] = $pRes->getMessage();
        } else {
            //all good
            if ($pRes->_transmitted == $pRes->_received) {
                $result['code'] = 0;
                $result['text'] = "RTT: " . $pRes->_round_trip['avg'] . " ms";
            } elseif ($pRes->_received == 0) {
                $result['code'] = 1;
                $result['text'] = "Offline";
            } else {
                $result['code'] = 3;
                $result['text'] = "Unknown error";
            }
        }
    }
    //exit codes
    // 0 = online
    // 1 = offline
    // 2 = error
    // 3 = unknown error
    //return result for web or cmd
    if (!$exit) {
        return $result;
    } else {
        exit($result['code']);
    }
}
Beispiel #13
0
 /**
  * Factory for Net_Ping
  *
  * @access public
  */
 public static function factory()
 {
     $ping_path = '';
     $sysname = Net_Ping::_setSystemName();
     if (($ping_path = Net_Ping::_setPingPath($sysname)) == NET_PING_CANT_LOCATE_PING_BINARY) {
         return $this->pear->raiseError(NET_PING_CANT_LOCATE_PING_BINARY_MSG, NET_PING_CANT_LOCATE_PING_BINARY);
     } else {
         return new Net_Ping($ping_path, $sysname);
     }
 }
Beispiel #14
0
        // set the timeout
        socket_set_option($this->icmp_socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => $timeout, "usec" => 0));
        if ($dst_addr) {
            if (@socket_connect($this->icmp_socket, $dst_addr, NULL)) {
            } else {
                $this->errstr = "Cannot connect to {$dst_addr}";
                return FALSE;
            }
            $this->Build_Packet();
            $this->start_time();
            socket_write($this->icmp_socket, $this->request, $this->request_len);
            if (@socket_recv($this->icmp_socket, &$this->reply, 256, 0)) {
                $this->time = $this->get_time($percision);
                return $this->time;
            } else {
                $this->errstr = "Timed out";
                return FALSE;
            }
        } else {
            $this->errstr = "Destination address not specified";
            return FALSE;
        }
    }
}
$ping = new Net_Ping();
$ping->ping("www.google.ca");
if ($ping->time) {
    echo "Time: " . $ping->time;
} else {
    echo $ping->errstr;
}
Beispiel #15
0
function ping($ip)
{
    include_once $_CONF[root_dir] . $_CONF[pear_dir] . "Net/Ping.php";
    $pingdata = array();
    $ping = Net_Ping::factory();
    if (PEAR::isError($ping)) {
        echo $ping->getMessage();
    } else {
        $ping->setArgs(array('count' => 2, 'size' => 32, 'deadline' => 1, 'timeout' => '4'));
        $ret = $ping->ping($ip);
        if (!$ret->getBytesTotal() > 0) {
            sleep(2);
            $ret = $ping->ping($ip);
        }
        $pingdata[alertType] = 'serverDown';
        if (get_class($ret) == 'Net_Ping_Result') {
            if ($ret->getBytesTotal() > 0) {
                $pingdata[alertSymbol] = 'ok';
                $pingdata[alertNote] = 'server up (ping passed)';
                $pingdata[alertText] = implode("\n", $ret->getRawData());
                $pingdata['Time'] = $ret->getAvg();
            } else {
                $pingdata[alertSymbol] = 'error';
                $pingdata[alertNote] = 'server down (ping failed)';
                $pingdata[alertText] = implode("\n", $ret->getRawData());
                $pingdata['Time'] = -100;
            }
        } else {
            if (get_class($ret) == 'PEAR_Error') {
                $pingdata[alertSymbol] = 'error';
                $pingdata[alertNote] = $ret->getMessage();
                $pingdata[alertText] = $ret->getMessage();
                $pingdata['Time'] = -100;
            }
        }
    }
    return $pingdata;
}
Beispiel #16
0
function device_display_general($device, $device_text) {
	global $colors;
	require(CACTI_BASE_PATH . "/include/data_query/data_query_arrays.php");
	require_once(CACTI_BASE_PATH . "/lib/device/device_info.php");

	if (isset($device["id"])) {
		html_start_box($device_text, "100", $colors["header"], "3", "center", "", true);
		?>
			<tr>
				<?php if (($device["availability_method"] == AVAIL_SNMP) ||
					($device["availability_method"] == AVAIL_SNMP_AND_PING) ||
					($device["availability_method"] == AVAIL_SNMP_OR_PING)) { ?>
				<td class="textInfo">
					<?php print __("SNMP Information");?><br>
					<span class="normal">
					<?php
					if ((($device["snmp_community"] == "") && ($device["snmp_username"] == "")) ||
						($device["snmp_version"] == 0)) {
						print "<span class=\"info\">SNMP not in use</span>\n";
					}else{
						$snmp_system = cacti_snmp_get($device["hostname"], $device["snmp_community"], ".1.3.6.1.2.1.1.1.0", $device["snmp_version"],
							$device["snmp_username"], $device["snmp_password"],
							$device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"],
							$device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], read_config_option("snmp_retries"),SNMP_WEBUI);

						/* modify for some system descriptions */
						/* 0000937: System output in devices.php poor for Alcatel */
						if (substr_count($snmp_system, "00:")) {
							$snmp_system = str_replace("00:", "", $snmp_system);
							$snmp_system = str_replace(":", " ", $snmp_system);
						}

						if ($snmp_system == "") {
							print "<span class=\"warning\">SNMP error</span>\n";
						}else{
							$snmp_uptime   = cacti_snmp_get($device["hostname"], $device["snmp_community"], ".1.3.6.1.2.1.1.3.0", $device["snmp_version"],
								$device["snmp_username"], $device["snmp_password"],
								$device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"],
								$device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);

							$snmp_hostname = cacti_snmp_get($device["hostname"], $device["snmp_community"], ".1.3.6.1.2.1.1.5.0", $device["snmp_version"],
								$device["snmp_username"], $device["snmp_password"],
								$device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"],
								$device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);

							$snmp_location = cacti_snmp_get($device["hostname"], $device["snmp_community"], ".1.3.6.1.2.1.1.6.0", $device["snmp_version"],
								$device["snmp_username"], $device["snmp_password"],
								$device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"],
								$device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);

							$snmp_contact  = cacti_snmp_get($device["hostname"], $device["snmp_community"], ".1.3.6.1.2.1.1.4.0", $device["snmp_version"],
								$device["snmp_username"], $device["snmp_password"],
								$device["snmp_auth_protocol"], $device["snmp_priv_passphrase"], $device["snmp_priv_protocol"],
								$device["snmp_context"], $device["snmp_port"], $device["snmp_timeout"], read_config_option("snmp_retries"), SNMP_WEBUI);

							print "<strong>System:</strong> " . html_split_string($snmp_system,200) . "<br>\n";
							$days      = intval($snmp_uptime / (60*60*24*100));
							$remainder = $snmp_uptime % (60*60*24*100);
							$hours     = intval($remainder / (60*60*100));
							$remainder = $remainder % (60*60*100);
							$minutes   = intval($remainder / (60*100));
							print "<strong>" . __("Uptime:")   . " </strong> $snmp_uptime";
							print "&nbsp;($days days, $hours hours, $minutes minutes)<br>\n";
							print "<strong>" . __("Hostname:") . " </strong> $snmp_hostname<br>\n";
							print "<strong>" . __("Location:") . " </strong> $snmp_location<br>\n";
							print "<strong>" . __("Contact:")  . " </strong> $snmp_contact<br>\n";
						}
					}
					?>
					</span>
				</td>
				<?php }
				if (($device["availability_method"] == AVAIL_PING) ||
					($device["availability_method"] == AVAIL_SNMP_AND_PING) ||
					($device["availability_method"] == AVAIL_SNMP_OR_PING)) {
					/* create new ping socket for device pinging */
					$ping = new Net_Ping;

					$ping->device = $device;
					$ping->port = $device["ping_port"];

					/* perform the appropriate ping check of the device */
					if ($ping->ping($device["availability_method"], $device["ping_method"],
						$device["ping_timeout"], $device["ping_retries"])) {
						$device_down = false;
						$ping_class = "ping";
						}else{
						$device_down = true;
						$ping_class = "ping_warning";
						}

				?>
				<td class="textInfo" style="vertical-align:top;">
					<?php print __("Ping Results");?><br>
					<span class="<?php $ping_class ?>">
					<?php print $ping->ping_response; ?>
					</span>
				</td>
				<?php }else if ($device["availability_method"] == AVAIL_NONE) { ?>
				<td class="textInfo">
					<?php print __("No Availability Check In Use");?><br>
				</td>
				<?php } ?>
			</tr>
		<?php
	}else{
		html_start_box($device_text, "100", $colors["header"], "3", "center", "", false);
	}

	html_end_box(FALSE);

	print "<form method='post' action='" .  basename($_SERVER["PHP_SELF"]) . "' name='device_edit_settings'>\n";
	html_start_box("<strong>" . __("General Settings") . "</strong>", "100", $colors["header"], 0, "center", "", true);
	$header_items = array(__("Field"), __("Value"));
	print "<tr><td>";
	html_header($header_items, 1, true, 'device');

	/* preserve the device template id if passed in via a GET variable */
	$fields_device_edit = device_form_list();
	if (!empty($_GET["template_id"])) {
//		$fields_device_edit["device_template_id"]["value"] = $_GET["template_id"];
//		$fields_device_edit["device_template_id"]["method"] = "hidden";
	}

	/* if we are creating a device and have changed templates set that value */
	if (!isset($device["id"])) {
		if (!empty($_GET["template_id"])) {
			$device["device_template_id"] = $_GET["template_id"];
		}
	}

	/* draw basic fields only on first run for a new device */
	draw_edit_form(array(
		"config" => array("form_name" => "chk", "no_form_tag" => true),
		"fields" => inject_form_variables($fields_device_edit, (is_array($device) ? $device : array()))
		));

	/* if the device is new, check/set the $device array with some template values */
	$override_permitted  = true;
	$propagation_allowed = false;
	if (!isset($device["id"])) {
		$template_settings = db_fetch_row("SELECT * FROM device_template WHERE id=" . $_REQUEST["template_id"]);
		if (sizeof($template_settings)) {
		foreach($template_settings as $key => $value) {
			switch($key) {
				case "id":
				case "name":
				case "description":
				case "hash":
				case "image":
					unset($template_settings[$key]);
					break;
				case "override_defaults":
					if ($value == CHECKED) {
						$propagation_allowed = true;
					}
					unset($template_settings[$key]);
					break;
				case "override_permitted":
					if ($value != CHECKED) {
						$override_permitted = false;
					}
					break;
				default:
					break;
			}
		}
		}
	}else{
		if (db_fetch_cell("SELECT override_defaults FROM device_template WHERE id=" . $device["device_template_id"]) == CHECKED) {
			$propagation_allowed = true;
		}
	}

	/* for a given device, display all availability options as well */
	draw_edit_form(array(
		"config" => array("form_name" => "chk", "no_form_tag" => true),
		"fields" => inject_form_variables(device_availability_form_list(), (isset($template_settings) ? $template_settings : $device))
		));

	print "</table></td></tr>";		/* end of html_header */
	html_end_box(!isset($device["id"]));
	form_hidden_box("id", (isset($device["id"]) ? $device["id"] : "0"), "");
	form_hidden_box("hidden_device_template_id", (isset($device["device_template_id"]) ? $device["device_template_id"] : "0"), "");
	form_hidden_box("save_basic_device", "1", "");
	form_hidden_box("save_component_device", "1", "");
	form_hidden_box("override_permitted", ($override_permitted ? "true":"false"), "");
	form_hidden_box("propagation_allowed", ($propagation_allowed ? "true":"false"), "");

	/* javascript relates to availability options, so include it only for existing devices */
	?>
	<script type="text/javascript">
	<!--

	// default snmp information
	var snmp_community       = document.getElementById('snmp_community').value;
	var snmp_username        = document.getElementById('snmp_username').value;
	var snmp_password        = document.getElementById('snmp_password').value;
	var snmp_auth_protocol   = document.getElementById('snmp_auth_protocol').value;
	var snmp_priv_passphrase = document.getElementById('snmp_priv_passphrase').value;
	var snmp_priv_protocol   = document.getElementById('snmp_priv_protocol').value;
	var snmp_context         = document.getElementById('snmp_context').value;
	var snmp_port            = document.getElementById('snmp_port').value;
	var snmp_timeout         = document.getElementById('snmp_timeout').value;
	var max_oids             = document.getElementById('max_oids').value;

	// default ping methods
	var ping_method    = document.getElementById('ping_method').value;
	var ping_port      = document.getElementById('ping_port').value;
	var ping_timeout   = document.getElementById('ping_timeout').value;
	var ping_retries   = document.getElementById('ping_retries').value;

	var availability_methods = document.getElementById('availability_method').options;
	var num_methods          = document.getElementById('availability_method').length;
	var selectedIndex        = document.getElementById('availability_method').selectedIndex;

	var agent = navigator.userAgent;
	agent = agent.match("MSIE");

	function setPingVisibility() {
		availability_method = document.getElementById('availability_method').value;
		ping_method         = document.getElementById('ping_method').value;

		/* debugging, uncomment as required */
		//alert("The availability method is '" + availability_method + "'");
		//alert("The ping method is '" + ping_method + "'");

		switch(availability_method) {
		case "0": // none
			document.getElementById('row_ping_method').style.display  = "none";
			document.getElementById('row_ping_port').style.display    = "none";
			document.getElementById('row_ping_timeout').style.display = "none";
			document.getElementById('row_ping_retries').style.display = "none";

			break;
		case "2": // snmp
			document.getElementById('row_ping_method').style.display  = "none";
			document.getElementById('row_ping_port').style.display    = "none";
			document.getElementById('row_ping_timeout').style.display = "";
			document.getElementById('row_ping_retries').style.display = "";

			break;
		default: // ping ok
			switch(ping_method) {
			case "1": // ping icmp
				document.getElementById('row_ping_method').style.display  = "";
				document.getElementById('row_ping_port').style.display    = "none";
				document.getElementById('row_ping_timeout').style.display = "";
				document.getElementById('row_ping_retries').style.display = "";

				break;
			case "2": // ping udp
			case "3": // ping tcp
				document.getElementById('row_ping_method').style.display  = "";
				document.getElementById('row_ping_port').style.display    = "";
				document.getElementById('row_ping_timeout').style.display = "";
				document.getElementById('row_ping_retries').style.display = "";

				break;
			}

			break;
		}
	}

	function addSelectItem(item, formObj) {
		if (agent != "MSIE") {
			formObj.add(item,null); // standards compliant
		}else{
			formObj.add(item);      // IE only
		}
	}

	function setAvailability(type) {
		/* get the availability structure */
		var am=document.getElementById('availability_method');

		/* get current selectedIndex */
		selectedIndex = document.getElementById('availability_method').selectedIndex;

		/* debugging uncomment as required */
		//alert("The selectedIndex is '" + selectedIndex + "'");
		//alert("The array length is '" + am.length + "'");

		switch(type) {
		case "NoSNMP":
			/* remove snmp options */
			if (am.length == 4) {
				am.remove(1);
				am.remove(1);
				am.remove(1);
			}

			/* set the index to something valid, like "ping" */
			if (selectedIndex > 1) {
				am.selectedIndex=1;
			}

			break;
		case "All":
			/* restore all options */
			if (am.length == 2) {
				am.remove(0);
				am.remove(0);

				var a=document.createElement('option');
				var b=document.createElement('option');
				var c=document.createElement('option');
				var d=document.createElement('option');
				var e=document.createElement('option');

				a.value="0";
				a.text="None";
				addSelectItem(a,am);

				b.value="1";
				b.text="Ping and SNMP";
				addSelectItem(b,am);

				e.value="4";
				e.text="Ping or SNMP";
				addSelectItem(e,am);

				c.value="2";
				c.text="SNMP";
				addSelectItem(c,am);

				d.value="3";
				d.text="Ping";
				addSelectItem(d,am);

				/* restore the correct index number */
				if (selectedIndex == 0) {
					am.selectedIndex = 0;
				}else{
					am.selectedIndex = 3;
				}
			}

			break;
		}

		setAvailabilityVisibility(type, am.selectedIndex);
		setPingVisibility();
	}

	function setAvailabilityVisibility(type, selectedIndex) {
		switch(type) {
		case "NoSNMP":
			switch(selectedIndex) {
			case "0": // availability none
				document.getElementById('row_ping_method').style.display="none";
				document.getElementById('ping_method').value=0;

				break;
			case "1": // ping
				document.getElementById('row_ping_method').style.display="";
				document.getElementById('ping_method').value=ping_method;

				break;
			}
		case "All":
			switch(selectedIndex) {
			case "0": // availability none
				document.getElementById('row_ping_method').style.display="none";
				document.getElementById('ping_method').value=0;

				break;
			case "1": // ping and snmp
			case "3": // ping
			case "4": // ping or snmp
				if ((document.getElementById('row_ping_method').style.display == "none") ||
					(document.getElementById('row_ping_method').style.display == undefined)) {
					document.getElementById('ping_method').value=ping_method;
					document.getElementById('row_ping_method').style.display="";
				}

				break;
			case "2": // snmp
				document.getElementById('row_ping_method').style.display="none";
				document.getElementById('ping_method').value="0";

				break;
			}
		}
	}

	function changeHostForm() {
		snmp_version        = document.getElementById('snmp_version').value;

		switch(snmp_version) {
		case "0":
			setAvailability("NoSNMP");
			setSNMP("None");

			break;
		case "1":
		case "2":
			setAvailability("All");
			setSNMP("v1v2");

			break;
		case "3":
			setAvailability("All");
			setSNMP("v3");

			break;
		}
	}

	function setSNMP(snmp_type) {
		switch(snmp_type) {
		case "None":
			document.getElementById('row_snmp_username').style.display        = "none";
			document.getElementById('row_snmp_password').style.display        = "none";
			document.getElementById('row_snmp_community').style.display       = "none";
			document.getElementById('row_snmp_auth_protocol').style.display   = "none";
			document.getElementById('row_snmp_priv_passphrase').style.display = "none";
			document.getElementById('row_snmp_priv_protocol').style.display   = "none";
			document.getElementById('row_snmp_context').style.display         = "none";
			document.getElementById('row_snmp_port').style.display            = "none";
			document.getElementById('row_snmp_timeout').style.display         = "none";
			document.getElementById('row_max_oids').style.display             = "none";

			break;
		case "v1v2":
			document.getElementById('row_snmp_username').style.display        = "none";
			document.getElementById('row_snmp_password').style.display        = "none";
			document.getElementById('row_snmp_community').style.display       = "";
			document.getElementById('row_snmp_auth_protocol').style.display   = "none";
			document.getElementById('row_snmp_priv_passphrase').style.display = "none";
			document.getElementById('row_snmp_priv_protocol').style.display   = "none";
			document.getElementById('row_snmp_context').style.display         = "none";
			document.getElementById('row_snmp_port').style.display            = "";
			document.getElementById('row_snmp_timeout').style.display         = "";
			document.getElementById('row_max_oids').style.display             = "";

			break;
		case "v3":
			document.getElementById('row_snmp_username').style.display        = "";
			document.getElementById('row_snmp_password').style.display        = "";
			document.getElementById('row_snmp_community').style.display       = "none";
			document.getElementById('row_snmp_auth_protocol').style.display   = "";
			document.getElementById('row_snmp_priv_passphrase').style.display = "";
			document.getElementById('row_snmp_priv_protocol').style.display   = "";
			document.getElementById('row_snmp_context').style.display         = "";
			document.getElementById('row_snmp_port').style.display            = "";
			document.getElementById('row_snmp_timeout').style.display         = "";
			document.getElementById('row_max_oids').style.display             = "";

			break;
		}
	}

	function toggleAvailabilityAndSnmp(template_enabled) {
		if (!template_enabled && $('#override_permitted').val() == 'true') {
			$('#override_permitted').removeAttr("disabled");
			$('#availability_header').removeAttr("disabled");
			$('#availability_method').removeAttr("disabled");
			$('#ping_method').removeAttr("disabled");
			$('#ping_port').removeAttr("disabled");
			$('#ping_timeout').removeAttr("disabled");
			$('#ping_retries').removeAttr("disabled");
			$('#snmp_spacer').removeAttr("disabled");
			$('#snmp_version').removeAttr("disabled");
			$('#snmp_username').removeAttr("disabled");
			$('#snmp_password').removeAttr("disabled");
			$('#snmp_password_confirm').removeAttr("disabled");
			$('#snmp_community').removeAttr("disabled");
			$('#snmp_auth_protocol').removeAttr("disabled");
			$('#snmp_priv_passphrase').removeAttr("disabled");
			$('#snmp_priv_protocol').removeAttr("disabled");
			$('#snmp_context').removeAttr("disabled");
			$('#snmp_port').removeAttr("disabled");
			$('#snmp_timeout').removeAttr("disabled");
			$('#max_oids').removeAttr("disabled");
			$('#device_threads').removeAttr("disabled");
		}else{
			$('#override_permitted').attr("disabled","disabled");
			$('#availability_header').attr("disabled","disabled");
			$('#availability_method').attr("disabled","disabled");
			$('#ping_method').attr("disabled","disabled");
			$('#ping_port').attr("disabled","disabled");
			$('#ping_timeout').attr("disabled","disabled");
			$('#ping_retries').attr("disabled","disabled");
			$('#snmp_spacer').attr("disabled","disabled");
			$('#snmp_version').attr("disabled","disabled");
			$('#snmp_username').attr("disabled","disabled");
			$('#snmp_password').attr("disabled","disabled");
			$('#snmp_password_confirm').attr("disabled","disabled");
			$('#snmp_community').attr("disabled","disabled");
			$('#snmp_auth_protocol').attr("disabled","disabled");
			$('#snmp_priv_passphrase').attr("disabled","disabled");
			$('#snmp_priv_protocol').attr("disabled","disabled");
			$('#snmp_context').attr("disabled","disabled");
			$('#snmp_port').attr("disabled","disabled");
			$('#snmp_timeout').attr("disabled","disabled");
			$('#max_oids').attr("disabled","disabled");
			$('#device_threads').attr("disabled","disabled");
		}

		changeHostForm();

		if ($('#override_permitted').val() == 'false') {
			$('#template_enabled').attr("checked","checked");
			$('#template_enabled').attr("disabled","disabled");
		}

		if ($('#propagation_allowed').val() == 'false') {
			$('#row_template_enabled').hide();
		}else{
			$('#row_template_enabled').show();
		}
	}

	$().ready(function() {
		toggleAvailabilityAndSnmp(document.getElementById('template_enabled').checked);

		/* Hide options when override is turned off */
		$("#template_enabled").change(function () {
			toggleAvailabilityAndSnmp(this.checked);
		});

		if ($('#id').val() == 0) {
			$('#device_template_id').change(function() {
				document.location='devices.php?action=edit&template_id='+this.value+'&status=-1'
			});
		}
	});

	-->
	</script>
	<?php

	if ((isset($_GET["display_dq_details"])) && (isset($_SESSION["debug_log"]["data_query"]))) {
		html_start_box("<strong>" . __("Data Query Debug Information") . "</strong>", "100", $colors["header"], "3", "center", "", true);

		print "<tr><td><span class=\"log\">" . debug_log_return("data_query") . "</span></td></tr>";

		html_end_box(false);
	}

	if (isset($device["id"])) {
		html_start_box("<strong>". __("Associated Graph Templates") . "</strong>", "100", $colors["header"], 0, "center", "", true);
		print "<tr><td>";
		html_header(array(__("Graph Template Name"), __("Status")), 2);

		$selected_graph_templates = db_fetch_assoc("select
			graph_templates.id,
			graph_templates.name
			from (graph_templates,device_graph)
			where graph_templates.id=device_graph.graph_template_id
			and device_graph.device_id=" . $_GET["id"] . "
			order by graph_templates.name");

		$available_graph_templates = db_fetch_assoc("SELECT
			graph_templates.id, graph_templates.name
			FROM snmp_query_graph RIGHT JOIN graph_templates
			ON (snmp_query_graph.graph_template_id = graph_templates.id)
			WHERE (((snmp_query_graph.name) Is Null)) ORDER BY graph_templates.name");

		/* omit those graph_templates, that have already been associated */
		$keeper = array();
		foreach ($available_graph_templates as $item) {
			if (sizeof(db_fetch_assoc("SELECT graph_template_id FROM device_graph " .
					" WHERE ((device_id=" . $_GET["id"] . ")" .
					" AND (graph_template_id=" . $item["id"] ."))")) > 0) {
				/* do nothing */
			} else {
				array_push($keeper, $item);
			}
		}

		$available_graph_templates = $keeper;

		$i = 0;
		if (sizeof($selected_graph_templates) > 0) {
		foreach ($selected_graph_templates as $item) {
			$i++;
			form_alternate_row_color("graph_template" . $i);

			/* get status information for this graph template */
			$is_being_graphed = (sizeof(db_fetch_assoc("select id from graph_local where graph_template_id=" . $item["id"] . " and device_id=" . $_GET["id"])) > 0) ? true : false;

			?>
				<td style="padding: 4px;">
					<strong><?php print $i;?>)</strong> <?php print $item["name"];?>
				</td>
				<td>
					<?php print (($is_being_graphed == true) ? "<span class=\"success\">" . __("Is Being Graphed") . "</span> (<a href='" . htmlspecialchars("graphs.php?action=graph_edit&id=" . db_fetch_cell("select id from graph_local where graph_template_id=" . $item["id"] . " and device_id=" . get_request_var("id") . " limit 0,1")) . "'>" . __("Edit") . "</a>)" : "<span class=\"unknown\">" . __("Not Being Graphed") . "</span>");?>
				</td>
				<td align='right' nowrap>
					<a href='devices.php?action=gt_remove&amp;id=<?php print $item["id"];?>&amp;device_id=<?php print $_GET["id"];?>'><img align='middle' class='buttonSmall' src='images/delete_icon_large.gif' title='<?php print __("Delete Graph Template Association");?>' alt='<?php print __("Delete");?>'></a>
				</td>
			<?php
			form_end_row();
		}
		}else{
			print "<tr><td><em>" . __("No Associated Graph Templates.") . "</em></td></tr>";
		}

		form_alternate_row_color("gt_device" . $device["id"]);
		?>
			<td colspan="4">
				<table cellspacing="0" cellpadding="1" width="100%">
					<tr>
					<td nowrap><?php print __("Add Graph Template:");?>&nbsp;
						<?php form_dropdown("graph_template_id",$available_graph_templates,"name","id","","","");?>
					</td>
					<td align="right">
						&nbsp;<input type="submit" value="<?php print __("Add");?>" name="add_gt_y" align="middle">
					</td>
					</tr>
				</table>
			</td>
		<?php
		form_end_row();
		print "</table></td></tr>";		/* end of html_header */
		html_end_box(FALSE);

		html_start_box("<strong>" . __("Associated Data Queries") . "</strong>", "100", $colors["header"], 0, "center", "", true);
		print "<tr><td>";
		html_header(array(__("Data Query Name"), __("Debugging"), __("Re-Index Method"), __("Status")), 2);

		$selected_data_queries = db_fetch_assoc("select
			snmp_query.id,
			snmp_query.name,
			device_snmp_query.reindex_method
			from (snmp_query,device_snmp_query)
			where snmp_query.id=device_snmp_query.snmp_query_id
			and device_snmp_query.device_id=" . $_GET["id"] . "
			order by snmp_query.name");

		$available_data_queries = db_fetch_assoc("select
			snmp_query.id,
			snmp_query.name
			from snmp_query
			order by snmp_query.name");

		$keeper = array();
		foreach ($available_data_queries as $item) {
			if (sizeof(db_fetch_assoc("SELECT snmp_query_id FROM device_snmp_query " .
					" WHERE ((device_id=" . $_GET["id"] . ")" .
					" and (snmp_query_id=" . $item["id"] ."))")) > 0) {
				/* do nothing */
			} else {
				array_push($keeper, $item);
			}
		}

		$available_data_queries = $keeper;

		$i = 0;
		if (sizeof($selected_data_queries) > 0) {
			foreach ($selected_data_queries as $item) {
				$i++;
				form_alternate_row_color("selected_data_queries" . $i);

				/* get status information for this data query */
				$num_dq_items = sizeof(db_fetch_assoc("select snmp_index from device_snmp_cache where device_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"]));
				$num_dq_rows = sizeof(db_fetch_assoc("select snmp_index from device_snmp_cache where device_id=" . $_GET["id"] . " and snmp_query_id=" . $item["id"] . " group by snmp_index"));

				$status = "success";

				?>
					<td style="padding: 4px;">
						<strong><?php print $i;?>)</strong> <?php print $item["name"];?>
					</td>
					<td>
						(<a href="devices.php?action=query_verbose&amp;id=<?php print $item["id"];?>&amp;device_id=<?php print $_GET["id"];?>"><?php print __("Verbose Query");?></a>)
					</td>
					<td>
						<?php form_dropdown("reindex_method_device_".get_request_var("id")."_query_".$item["id"]."_method_".$item["reindex_method"],$reindex_types,"","",$item["reindex_method"],"","","","");?>
					</td>
					<td>
						<?php print (($status == "success") ? "<span class=\"success\">" . __("Success") . "</span>" : "<span class=\"fail\">" . __("Fail") . "</span>");?> [<?php print $num_dq_items;?> <?php print __("Item", $num_dq_items);?>, <?php print $num_dq_rows;?> <?php print __("Row", $num_dq_rows);?>]
					</td>
					<td align='right' nowrap>
						<a href='devices.php?action=query_reload&amp;id=<?php print $item["id"];?>&amp;device_id=<?php print $_GET["id"];?>'><img align='middle' class='buttonSmall' src='images/reload_icon_small.gif' title='<?php print __("Reload Data Query");?>' alt='<?php print __("Reload");?>'></a>&nbsp;
						<a href='devices.php?action=query_remove&amp;id=<?php print $item["id"];?>&amp;device_id=<?php print $_GET["id"];?>'><img align='middle' class='buttonSmall' src='images/delete_icon_large.gif' title='<?php print __("Delete Data Query Association");?>' alt='<?php print __("Delete");?>'></a>
					</td>
				<?php
				form_end_row();
			}
		}else{
			print "<tr><td><em>". __("No associated data queries.") . "</em></td></tr>";
		}

		form_alternate_row_color("dq_device" . $device["id"]);

		?>
			<td colspan="5">
				<table cellspacing="0" cellpadding="1" width="100%">
					<tr>
					<td nowrap><?php print __("Add Data Query:");?>&nbsp;
						<?php form_dropdown("snmp_query_id",$available_data_queries,"name","id","","","");?>
					</td>
					<td nowrap><?php print __("Re-Index Method:");?>&nbsp;
						<?php form_dropdown("reindex_method",$reindex_types,"","","1","","");?>
					</td>
					<td align="right">
						&nbsp;<input type="submit" value="<?php print __("Add");?>" name="add_dq_y" align="middle">
					</td>
					</tr>
				</table>
			</td>
		<?php
		form_end_row();
		print "</table></td></tr>";		/* end of html_header */
		html_end_box();
	}

	form_save_button_alt();
}