1<?php
2
3global $config;
4
5if (!isset($called_by_script_server)) {
6	include_once(dirname(__FILE__) . '/../include/cli_check.php');
7	include_once(dirname(__FILE__) . '/../lib/snmp.php');
8
9	array_shift($_SERVER['argv']);
10
11	print call_user_func_array('ss_net_snmp_disk_bytes', $_SERVER['argv']);
12} else {
13	include_once(dirname(__FILE__) . '/../lib/snmp.php');
14}
15
16function ss_net_snmp_disk_bytes($host_id_or_hostname = '') {
17	global $environ, $poller_id, $config;
18
19	if (empty($host_id_or_hostname) || $host_id_or_hostname === NULL) {
20		return 'reads:0 writes:0';
21	} elseif (!is_numeric($host_id_or_hostname)) {
22		$host_id = db_fetch_cell_prepared('SELECT id
23			FROM host
24			WHERE hostname = ?',
25			array($host_id_or_hostname));
26	} else {
27		$host_id = $host_id_or_hostname;
28	}
29
30	$tmpdir = sys_get_temp_dir();
31
32	if ($environ != 'realtime') {
33		$tmpdir = $tmpdir . '/cacti/net-snmp-devio';
34		$tmpfile = $host_id . '_bytes';
35	} else {
36		$tmpdir = $tmpdir . '/cacti-rt/net-snmp-devio';
37		$tmpfile = $host_id . '_' . $poller_id . '_bytes_rt';
38	}
39
40	if (!is_dir($tmpdir)) {
41		mkdir($tmpdir, 0777, true);
42	}
43
44	$found    = false;
45	$previous = array();
46
47	if (is_file("$tmpdir/$tmpfile")) {
48		$previous = json_decode(file_get_contents("$tmpdir/$tmpfile"), true);
49		$found = true;
50	}
51
52	$indexes = array();
53
54	$host = db_fetch_row_prepared('SELECT *
55		FROM host
56		WHERE id = ?',
57		array($host_id));
58
59	$uptime  = cacti_snmp_get($host['hostname'],
60		$host['snmp_community'],
61		'.1.3.6.1.2.1.1.3.0',
62		$host['snmp_version'],
63		$host['snmp_username'],
64		$host['snmp_password'],
65		$host['snmp_auth_protocol'],
66		$host['snmp_priv_passphrase'],
67		$host['snmp_priv_protocol'],
68		$host['snmp_context'],
69		$host['snmp_port'],
70		$host['snmp_timeout'],
71		$host['ping_retries'],
72		$host['max_oids'],
73		SNMP_POLLER,
74		$host['snmp_engine_id']);
75
76	$current['uptime'] = $uptime;
77
78	$names  = cacti_snmp_walk($host['hostname'],
79		$host['snmp_community'],
80		'.1.3.6.1.4.1.2021.13.15.1.1.2',
81		$host['snmp_version'],
82		$host['snmp_username'],
83		$host['snmp_password'],
84		$host['snmp_auth_protocol'],
85		$host['snmp_priv_passphrase'],
86		$host['snmp_priv_protocol'],
87		$host['snmp_context'],
88		$host['snmp_port'],
89		$host['snmp_timeout'],
90		$host['ping_retries'],
91		$host['max_oids'],
92		SNMP_POLLER,
93		$host['snmp_engine_id']);
94
95	foreach($names as $measure) {
96		if (substr($measure['value'],0,2) == 'sd') {
97			if (is_numeric(substr(strrev($measure['value']),0,1))) {
98				continue;
99			}
100
101			$parts = explode('.', $measure['oid']);
102			$indexes[$parts[cacti_sizeof($parts)-1]] = $parts[cacti_sizeof($parts)-1];
103		}
104	}
105
106	$bytesread = $byteswritten = 0;
107
108	if (cacti_sizeof($indexes)) {
109		$bytes = cacti_snmp_walk($host['hostname'],
110			$host['snmp_community'],
111			'.1.3.6.1.4.1.2021.13.15.1.1.12',
112			$host['snmp_version'],
113			$host['snmp_username'],
114			$host['snmp_password'],
115			$host['snmp_auth_protocol'],
116			$host['snmp_priv_passphrase'],
117			$host['snmp_priv_protocol'],
118			$host['snmp_context'],
119			$host['snmp_port'],
120			$host['snmp_timeout'],
121			$host['ping_retries'],
122			$host['max_oids'],
123			SNMP_POLLER,
124			$host['snmp_engine_id']);
125
126		foreach($bytes as $measure) {
127			$parts = explode('.', $measure['oid']);
128			$index = $parts[cacti_sizeof($parts)-1];
129
130			if (array_key_exists($index, $indexes)) {
131				if (!isset($previous['uptime'])) {
132					$bytesread = 'U';
133				} elseif ($current['uptime'] < $previous['uptime']) {
134					$bytesread = 'U';
135				} elseif (!isset($previous["br$index"])) {
136					$bytesread = 'U';
137				} elseif ($previous["br$index"] > $measure['value']) {
138					$bytesread += $measure['value'] + 18446744073709551615 - $previous["br$index"] - $previous["br$index"];
139				} else {
140					$bytesread += $measure['value'] - $previous["br$index"];
141				}
142
143				$current["br$index"] = $measure['value'];
144			}
145		}
146
147		$bytes = cacti_snmp_walk($host['hostname'],
148			$host['snmp_community'],
149			'.1.3.6.1.4.1.2021.13.15.1.1.13',
150			$host['snmp_version'],
151			$host['snmp_username'],
152			$host['snmp_password'],
153			$host['snmp_auth_protocol'],
154			$host['snmp_priv_passphrase'],
155			$host['snmp_priv_protocol'],
156			$host['snmp_context'],
157			$host['snmp_port'],
158			$host['snmp_timeout'],
159			$host['ping_retries'],
160			$host['max_oids'],
161			SNMP_POLLER,
162			$host['snmp_engine_id']);
163
164		foreach($bytes as $measure) {
165			$parts = explode('.', $measure['oid']);
166			$index = $parts[cacti_sizeof($parts)-1];
167
168			if (array_key_exists($index, $indexes)) {
169				if (!isset($previous['uptime'])) {
170					$byteswritten = 'U';
171				} elseif ($current['uptime'] < $previous['uptime']) {
172					$byteswritten = 'U';
173				} elseif (!isset($previous["bw$index"])) {
174					$byteswritten = 'U';
175				} elseif ($previous["bw$index"] > $measure['value']) {
176					$byteswritten += $measure['value'] + 18446744073709551615 - $previous["bw$index"] - $previous["bw$index"];
177				} else {
178					$byteswritten += $measure['value'] - $previous["bw$index"];
179				}
180
181				$current["bw$index"] = $measure['value'];
182			}
183		}
184
185		$data = "'" . json_encode($current) . "'";
186		shell_exec("echo $data > $tmpdir/$tmpfile");
187	}
188
189	if ($found) {
190		return "bytesread:$bytesread byteswritten:$byteswritten";
191	} else {
192		return 'bytesread:0 byteswritten:0';
193	}
194}
195
196