Skip to content

Commit

Permalink
Audit of PHP Collection Scripts
Browse files Browse the repository at this point in the history
- Double to single quotes
- Prepared Statements
- "\n" to PHP_EOL
  • Loading branch information
cigamit committed Apr 7, 2018
1 parent ddbc903 commit 7737c01
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 158 deletions.
14 changes: 7 additions & 7 deletions scripts/ss_count_oids.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ function ss_count_oids($hostid = '', $oid = '') {
include_once($config['base_path'] . '/lib/snmp.php');

if ($hostid > 0) {
$host = db_fetch_row("SELECT hostname, snmp_community, snmp_version, snmp_username, snmp_password,
snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context,
snmp_port, snmp_timeout, max_oids
FROM host
WHERE id=$hostid");
$host = db_fetch_row_prepared('SELECT hostname, snmp_community, snmp_version, snmp_username, snmp_password,
snmp_auth_protocol, snmp_priv_passphrase, snmp_priv_protocol, snmp_context,
snmp_port, snmp_timeout, max_oids, snmp_engine_id
FROM host
WHERE id = ?',
array($hostid));

if (sizeof($host)) {
$walk = cacti_snmp_walk($host['hostname'], $host['snmp_community'], $oid, $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'), $host['max_oids'], SNMP_WEBUI);
read_config_option('snmp_retries'), $host['max_oids'], SNMP_WEBUI, $host['snmp_engine_id']);

if (sizeof($walk)) {
return sizeof($walk);
Expand All @@ -39,4 +40,3 @@ function ss_count_oids($hostid = '', $oid = '') {
return '0';
}

?>
43 changes: 20 additions & 23 deletions scripts/ss_fping.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
$no_http_headers = true;

/* 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>");
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>');
}

/* display no errors */
error_reporting(0);

include_once(dirname(__FILE__) . "/../include/global.php");
include_once(dirname(__FILE__) . "/../lib/snmp.php");
include_once(dirname(__FILE__) . "/../lib/ping.php");
include_once(dirname(__FILE__) . '/../include/global.php');
include_once(dirname(__FILE__) . '/../lib/snmp.php');
include_once(dirname(__FILE__) . '/../lib/ping.php');

if (!isset($called_by_script_server)) {
array_shift($_SERVER["argv"]);
print call_user_func_array("ss_fping", $_SERVER["argv"]);
array_shift($_SERVER['argv']);
print call_user_func_array('ss_fping', $_SERVER['argv']);
}
//End header.

function ss_fping($hostname, $ping_sweeps=6, $ping_type="ICMP", $port=80) {
function ss_fping($hostname, $ping_sweeps=6, $ping_type='ICMP', $port=80) {
/* record start time */
list($micro,$seconds) = explode(" ", microtime());
list($micro,$seconds) = explode(' ', microtime());
$ss_fping_start = $seconds + $micro;

$ping = new Net_Ping;
Expand All @@ -32,34 +32,31 @@ function ss_fping($hostname, $ping_sweeps=6, $ping_type="ICMP", $port=80) {
$total_time = 0;
$failed_results = 0;

$ping->host["hostname"] = gethostbyname($hostname);
$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");
$script_timeout = read_config_option('script_timeout');
$ping_timeout = read_config_option('ping_timeout');

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

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

if (!$result) {
$failed_results++;
Expand All @@ -73,18 +70,18 @@ function ss_fping($hostname, $ping_sweeps=6, $ping_type="ICMP", $port=80) {
$i++;

/* get current time */
list($micro,$seconds) = explode(" ", microtime());
list($micro,$seconds) = explode(' ', 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)) {
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 "min:U avg:U max:U dev:U loss:100.00";
return 'min:U avg:U max:U dev:U loss:100.00';
} else {
$loss = ($failed_results/$ping_sweeps) * 100;
$avg = $total_time/($ping_sweeps-$failed_results);
Expand All @@ -96,7 +93,7 @@ function ss_fping($hostname, $ping_sweeps=6, $ping_type="ICMP", $port=80) {
}
$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);
return sprintf('min:%0.4f avg:%0.4f max:%0.4f dev:%0.4f loss:%0.4f', $min, $avg, $max, $dev, $loss);
}
}

10 changes: 5 additions & 5 deletions scripts/ss_host_cpu.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ function ss_host_cpu($hostname, $host_id, $snmp_auth, $cmd, $arg1 = '', $arg2 =
$arr_index = ss_host_cpu_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);

foreach($arr_index as $value) {
print $value . "\n";
print $value . PHP_EOL;
}
} else {
$indexes = explode(',', $value);
foreach($indexes as $index) {
print $index . "\n";
print $index . PHP_EOL;
}
}
} elseif (($cmd == 'num_indexes')) {
Expand All @@ -92,15 +92,15 @@ function ss_host_cpu($hostname, $host_id, $snmp_auth, $cmd, $arg1 = '', $arg2 =

foreach ($arr_index as $index => $value) {
if ($arg == 'usage') {
print $index . '!' . $arr[$index] . "\n";
print $index . '!' . $arr[$index] . PHP_EOL;
} elseif ($arg == 'index') {
print $index . '!' . $value . "\n";
print $index . '!' . $value . PHP_EOL;
}
}
} else {
$indexes = explode(',', $value);
foreach($indexes as $index) {
print $index . '!' . $index . "\n";
print $index . '!' . $index . PHP_EOL;
}
}
} elseif ($cmd == 'get') {
Expand Down
10 changes: 5 additions & 5 deletions scripts/ss_host_disk.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function ss_host_disk($hostname, $host_id, $snmp_auth, $cmd, $arg1 = '', $arg2 =
);

for ($i=0;($i<sizeof($return_arr));$i++) {
print $return_arr[$i] . "\n";
print $return_arr[$i] . PHP_EOL;
}
} elseif ($cmd == 'num_indexes') {
$return_arr = ss_host_disk_reindex(
Expand All @@ -94,7 +94,7 @@ function ss_host_disk($hostname, $host_id, $snmp_auth, $cmd, $arg1 = '', $arg2 =
);

for ($i=0;($i<sizeof($arr_index));$i++) {
print $arr_index[$i] . '!' . $arr[$i] . "\n";
print $arr_index[$i] . '!' . $arr[$i] . PHP_EOL;
}
} elseif ($cmd == 'get') {
$arg = $arg1;
Expand All @@ -104,11 +104,11 @@ function ss_host_disk($hostname, $host_id, $snmp_auth, $cmd, $arg1 = '', $arg2 =

if (is_array($value)) {
if (($arg == 'total') || ($arg == 'used')) {
$sau = preg_replace('/[^0-9]/i', '', db_fetch_cell_prepared("SELECT field_value
$sau = preg_replace('/[^0-9]/i', '', db_fetch_cell_prepared('SELECT field_value
FROM host_snmp_cache
WHERE host_id = ?
AND field_name = 'hrStorageAllocationUnits'
AND snmp_index = ?",
AND field_name = "hrStorageAllocationUnits"
AND snmp_index = ?',
array($host_id, $index)));

$snmp_data = cacti_snmp_get($hostname, $snmp_community, $oids[$arg] . ".$index", $snmp_version,
Expand Down
105 changes: 53 additions & 52 deletions scripts/ss_net_snmp_disk_bytes.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,39 +58,39 @@ function ss_net_snmp_disk_bytes($host_id_or_hostname) {
$indexes = array();
$host = db_fetch_row_prepared('SELECT * FROM host WHERE id = ?', array($host_id));

$uptime = cacti_snmp_get($host['hostname'],
$host['snmp_community'],
$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'],
$host['ping_retries'],
$host['max_oids'],
$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'],
$host['ping_retries'],
$host['max_oids'],
SNMP_POLLER,
$host['snmp_engine_id']);

$current['uptime'] = $uptime;

$names = cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
$names = cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
'.1.3.6.1.4.1.2021.13.15.1.1.2',
$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'],
$host['ping_retries'],
$host['max_oids'],
$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'],
$host['ping_retries'],
$host['max_oids'],
SNMP_POLLER,
$host['snmp_engine_id']);

Expand All @@ -108,20 +108,20 @@ function ss_net_snmp_disk_bytes($host_id_or_hostname) {
$bytesread = $byteswritten = 0;

if (sizeof($indexes)) {
$bytes = cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
$bytes = cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
'.1.3.6.1.4.1.2021.13.15.1.1.12',
$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'],
$host['ping_retries'],
$host['max_oids'],
$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'],
$host['ping_retries'],
$host['max_oids'],
SNMP_POLLER,
$host['snmp_engine_id']);

Expand All @@ -146,20 +146,20 @@ function ss_net_snmp_disk_bytes($host_id_or_hostname) {
}
}

$bytes = cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
$bytes = cacti_snmp_walk($host['hostname'],
$host['snmp_community'],
'.1.3.6.1.4.1.2021.13.15.1.1.13',
$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'],
$host['ping_retries'],
$host['max_oids'],
$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'],
$host['ping_retries'],
$host['max_oids'],
SNMP_POLLER,
$host['snmp_engine_id']);

Expand Down Expand Up @@ -194,3 +194,4 @@ function ss_net_snmp_disk_bytes($host_id_or_hostname) {
return 'bytesread:0 byteswritten:0';
}
}

Loading

0 comments on commit 7737c01

Please sign in to comment.