File: mib_cache.php

package info (click to toggle)
cacti 1.2.30%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,176 kB
  • sloc: php: 123,193; javascript: 29,825; sql: 2,595; xml: 1,823; sh: 1,228; perl: 194; makefile: 65; python: 51; ruby: 9
file content (424 lines) | stat: -rw-r--r-- 14,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2024 The Cacti Group                                 |
 |                                                                         |
 | 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.                            |
 +-------------------------------------------------------------------------+
 | Cacti: The Complete RRDtool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
 */

class MibCache{
	private $active_mib            = '';
	private $active_object         = '';
	private $active_table          = '';
	private $active_table_entry    = '';
	private $cache__tables         = array();
	private $cache__tables_columns = array();

	public function __construct($mib='CACTI-MIB') {
		$this->active_mib = $mib;
		return $this;
	}

	public function __destruct() {

	}

	public function uninstall() {
		/* avoid that our default mib will be dropped by some plugin developer */
		if ($this->active_mib == 'CACTI-MIB') {
			return false;
		} else {
			db_execute_prepared('DELETE FROM snmpagent_cache WHERE `mib` = ?', array($this->active_mib));
			db_execute_prepared('DELETE FROM snmpagent_cache_notifications WHERE `mib` = ?', array($this->active_mib));
			db_execute_prepared('DELETE FROM snmpagent_cache_textual_conventions WHERE `mib` = ?', array($this->active_mib));
			db_execute_prepared('DELETE FROM snmpagent_mibs WHERE `name` = ?', array($this->active_mib));
		}
	}

	public function install($path, $replace=false, $mib_name='optional') {
		global $config;

		include_once($config['include_path'] . '/vendor/phpsnmp/mib_parser.php');

		$mp = new MibParser();
		$mp->add_mib($path, $mib_name);
		$mp->generate();

		if (isset($mp->mib) && isset($mp->oids) && $mp->mib ) {
			/* check if this mib has already been installed */
			$existing = db_fetch_cell_prepared('SELECT 1 FROM snmpagent_mibs WHERE `name` = ?', array($mp->mib));
			if ($existing) {
				if ($replace == false) {
					unset($mp->oids);
					unset($mp->mib);
					return false;
				} else {
					$this->uninstall();
				}
			}
			db_execute_prepared('INSERT INTO snmpagent_mibs SET `id` = 0, `name` = ?, `file` = ?', array($mp->mib, $path));

			foreach($mp->oids as $object_name => $object_params) {
				if ($object_params['otype'] != 'TEXTUAL-CONVENTION') {
					db_execute_prepared('INSERT IGNORE INTO `snmpagent_cache`
						(`oid`, `name`, `mib`, `type`, `otype`, `kind`, `max-access`, `description`)
						VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
						array($object_params['oid'], $object_name, $object_params['mib'], $object_params['syntax'],
							$object_params['otype'], $object_params['kind'], $object_params['max-access'],
							str_replace("\r\n", '<br>', trim($object_params['description']))));

					if ($object_params['otype'] == 'NOTIFICATION-TYPE') {
						foreach($object_params['objects'] as $notification_object_index => $notification_object) {
							db_execute_prepared('INSERT INTO `snmpagent_cache_notifications`
								(`name`, `mib`, `attribute`, `sequence_id`)
								VALUES (?, ?, ?, ?)',
								array($object_name, $object_params['mib'], $notification_object, $notification_object_index));
						}
					}
				} else {
					db_execute_prepared('INSERT INTO `snmpagent_cache_textual_conventions`
						(`name`, `mib`, `type`, `description`)
						VALUES (?, ?, ?, ?)',
						array($object_name, $object_params['mib'], $object_params['syntax'], nl2br($object_params['description'])));
				}
			}

			unset($mp->oids);
			unset($mp->mib);
		} else {
			return false;
		}
	}

	public function mib($mib) {
		$this->active_mib = $mib;
		$this->active_object = '';
		$this->active_table = '';
		$this->active_table_entry = '';
		return $this;
	}

	public function object($object) {
		$this->active_object = $object;
		return $this;
	}

	public function table($table) {
		if ($this->active_table != $table) {
			if (!isset($this->cache__tables[$this->active_mib][$table])) {
				$oid_table = db_fetch_cell_prepared('SELECT oid
					FROM `snmpagent_cache`
					WHERE `mib` = ?
					AND `name` = ?
					AND `type` = "SEQUENCE OF"',
					array($this->active_mib, $table));

				if ($oid_table) {
					/* cache table oid and columns */
					$this->cache__tables[$this->active_mib][$table] = $oid_table;
					$this->active_table = $table;
					$this->cache__tables_columns[$this->active_mib][$table] = $this->columns();
					$this->active_table_entry = '';
					return $this;
				} else {
					/* MIB table does not exist */
					$this->active_table = '';
					$this->active_table_entry = '';
					throw new Exception('MIB table does not exist');
				}
			} else {
				/* table exists and has already been cached */
				$this->active_table = $table;
				$this->active_table_entry = '';
				return $this;
			}
		} else {
			/* no changes necessary */
			return $this;
		}
	}

	public function row($index) {
		/* limited to one single $index so far */
		$this->active_table_entry = $index;
		return $this;
	}

	public function gettype() {

	}

	public function set($value) {
		return db_execute_prepared('UPDATE `snmpagent_cache`
			SET `value` = ?
			WHERE `mib` = ?
			AND `name` = ?',
			array($value, $this->active_mib, $this->active_object));
	}

	public function get() {
		return db_fetch_row_prepared('SELECT *
			FROM snmpagent_cache
			WHERE name = ?
			AND mib = ?',
			array($this->active_object, $this->active_mib));
	}

	public function count() {
		return db_execute_prepared('UPDATE snmpagent_cache
			SET `value` = CASE
			WHEN `type`="Counter32" AND `value`= 4294967295 THEN 0
			WHEN `type`="Counter64" AND `value`= 18446744073709551615 THEN 0
			ELSE `value`+1 END
			WHERE `mib` = ? AND `name` = ?',
			array($this->active_mib, $this->active_object));
	}

	public function insert($values) {
		$oid_entry = $this->exists();
		if ($oid_entry == false) {
			$columns = $this->cache__tables_columns[$this->active_mib][$this->active_table];
			if ($columns && cacti_sizeof($columns) > 0) {
				foreach($columns as $column_params) {
					$column_params['oid'] .= '.' . $this->active_table_entry;
					$column_params['otype'] = 'DATA';

					if (isset($values[$column_params['name']])) {
						$column_params['value'] = $values[$column_params['name']];
					}

					db_execute_prepared('INSERT INTO `snmpagent_cache`
						(`oid`, `name`, `mib`, `type`, `otype`, `kind`, `max-access`, `value`)
						VALUES (?, ?, ?, ?, ?, ?, ?, ?)
						ON DUPLICATE KEY UPDATE `name`=VALUES(`name`), `mib`=VALUES(`mib`),
						`type`=VALUES(`type`), `otype`=VALUES(`otype`), `kind`=VALUES(`kind`),
						`max-access`=VALUES(`max-access`), `value`=VALUES(`value`)',
						array($column_params['oid'], $column_params['name'], $column_params['mib'],
							$column_params['type'], $column_params['otype'], 'Column Data',
							$column_params['max-access'], trim($column_params['value'])));
				}
				return true;
			}
		}
		return false;
	}

	public function select($column=false) {
		$result = array();
		if ($this->active_table_entry) {
			/* focus on a dedicated MIB table row only */
			$oid_entry = $this->exists();
			if ($oid_entry !== false) {
				if ($column == false) {
					/* fetch the whole row */
					$filter = $oid_entry . '.%.' . $this->active_table_entry;

					$entries =  db_fetch_assoc_prepared('SELECT name, value
						FROM snmpagent_cache
						WHERE oid LIKE ?
						GROUP BY name
						ORDER BY oid',
						array($filter));

					if ($entries && cacti_sizeof($entries)>0) {
						foreach($entries as $entry) { $result[$entry['name']] = $entry['value']; }
						return $result;
					}
				} elseif (is_string($column)) {
					/* fetch only the value of a given column */
					$filter = $oid_entry . '.%.' . $this->active_table_entry;

					return db_fetch_cell_prepared('SELECT value
						FROM snmpagent_cache
						WHERE name = ?
						AND oid LIKE ?
						LIMIT 1',
						array($column, $filter));
				} elseif (is_array($column) && cacti_sizeof($column)>0) {
					$filter = $oid_entry . '.%.' . $this->active_table_entry;

					/* fetch all values of specific columns given for that MIB table row */
					$entries = db_fetch_assoc_prepared("SELECT name, value
						FROM snmpagent_cache
						WHERE name IN ('" . implode("','", $column) . "')
						AND oid LIKE ?
						GROUP BY name
						ORDER BY oid",
						array($filter));

					if ($entries && cacti_sizeof($entries)>0) {
						foreach($entries as $entry) { $result[$entry['name']] = $entry['value']; }
						return $result;
					}
				}
			}
		} else {
			/* query the whole MIB table */
			$oid_entry = $this->cache__tables[$this->active_mib][$this->active_table] . '.1';
			if ($column == false) {
				/* fetch all rows */
				$columns     = $this->cache__tables_columns[$this->active_mib][$this->active_table];
				$num_columns = cacti_sizeof($columns);
				$filter      = $oid_entry . '.%.%';

				$entries = db_fetch_assoc_prepared('SELECT name, value
					FROM snmpagent_cache
					WHERE oid LIKE ?
					ORDER BY oid',
					array($filter));

				if ($num_columns && $entries && cacti_sizeof($entries)) {
					$num_entries = cacti_sizeof($entries);
					$entries_per_object = $num_entries/$num_columns;
					for($i = 0; $i < $entries_per_object; $i++) {
						$result[$i]=array();
						for($j=0; $j < $num_columns; $j++) {
							$result[$i][$entries[$i+$j*$entries_per_object]['name']] = $entries[$i+$j*$entries_per_object]['value'];
						}
					}
					return $result;
				} else {
					return $entries;
				}
			} elseif (is_string($column)) {
				/* fetch only the values of one single column */
				$filter = $oid_entry . '.%.%';

				return db_fetch_assoc_prepared("SELECT value AS '" . $column . "'
					FROM snmpagent_cache
					WHERE name = ?
					AND oid LIKE ?
					ORDER BY oid",
					array($column, $filter));
			} elseif (is_array($column) && cacti_sizeof($column)>0) {
				/* fetch values of specific columns given */
				$filter = $oid_entry . '.%.%';

				$entries = db_fetch_assoc_prepared("SELECT name, value
					FROM snmpagent_cache
					WHERE name IN ('" . implode("','", $column) . "')
					AND oid LIKE ?
					ORDER BY oid", array($filter));

				if (cacti_sizeof($entries)) {
					$num_objects = cacti_sizeof($column);
					$num_entries = cacti_sizeof($entries);
					$entries_per_object = ceil($num_entries/$num_objects);

					for($i = 0; $i < $entries_per_object; $i++) {
						$result[$i]=array();
						for($j=0; $j < $num_objects; $j++) {
							$index = (int) $i + ($j * $entries_per_object);
							$result[$i][$entries[$index]['name']] = $entries[$index]['value'];
						}
					}
					return $result;
				} else {
					return $entries;
				}
			}
		}

		return false;
	}

	public function delete() {
		$oid_entry = $this->exists();
		if ($oid_entry !== false) {
			/* get list of columns for this mib table */
			$columns = $this->cache__tables_columns[$this->active_mib][$this->active_table];
			if ($columns && cacti_sizeof($columns) > 0) {
				foreach($columns as $column_params) {
					$column_params['oid'] .= '.' . $this->active_table_entry;
					db_execute_prepared('DELETE FROM `snmpagent_cache` WHERE `oid` = ?', array($column_params['oid']));
				}
				return true;
			}
		}
		return false;
	}

	public function update($values) {
		$oid_entry = $this->exists();
		if ($oid_entry !== false) {
			$columns = $this->cache__tables_columns[$this->active_mib][$this->active_table];
			if (cacti_sizeof($columns)>0) {
				$sql = array();

				foreach($columns as $column_params) {
					$column_params['oid'] .= '.' . $this->active_table_entry;
					if (isset($values[$column_params['name']])) {
						$sql[] = '(' . db_qstr($column_params['name']) . ', ' . db_qstr($values[$column_params['name']]) . ', ' . db_qstr($column_params['oid']) . ')';
					}
				}

				if (cacti_sizeof($sql)) {
					db_execute('INSERT INTO `snmpagent_cache`
						(name, value, oid)
						VALUES ' . implode(', ', $sql) . '
						ON DUPLICATE KEY UPDATE value=VALUES(value)', $sql);
				}

				return true;
			}
		}

		return false;
	}

	public function replace($values) {
		$this->delete();
		return $this->insert($values);
	}

	public function truncate() {
		$oid_entry = $this->cache__tables[$this->active_mib][$this->active_table] . '.1.%';
		db_execute_prepared('DELETE FROM `snmpagent_cache`
			WHERE `mib` = ?
			AND `otype` = "DATA"
			AND `oid` LIKE ?',
			array($this->active_mib, $oid_entry));

		return true;
	}

	public function columns() {
		/* As defined by SMI the OID value assigned to the row must be the same as the OID value assigned to the table containing
		   the row with addition of a single value of one. */
		$filter = $this->cache__tables[$this->active_mib][$this->active_table] . '.1.%';

		return db_fetch_assoc_prepared('SELECT *
			FROM `snmpagent_cache`
			WHERE `oid` LIKE ?
			GROUP BY name
			ORDER BY oid',
			array($filter));
	}

	private function exists() {
		$oid_entry = $this->cache__tables[$this->active_mib][$this->active_table] . '.1';

		/* check if entry exists */
		$exists = db_fetch_cell_prepared('SELECT 1 FROM `snmpagent_cache` WHERE `oid` = ?',
			array($oid_entry . '.1.' . $this->active_table_entry));

		return ($exists) ? $oid_entry : false;
	}
}