File: devices.cpp

package info (click to toggle)
netmrg 0.20-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,856 kB
  • ctags: 2,009
  • sloc: php: 9,401; sh: 4,644; cpp: 2,898; perl: 621; ansic: 381; makefile: 331; xml: 92; sql: 71; sed: 16
file content (458 lines) | stat: -rw-r--r-- 13,813 bytes parent folder | download | duplicates (4)
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/********************************************
* NetMRG Integrator
*
* devices.cpp
* NetMRG Gatherer Devices Library
*
* see doc/LICENSE for copyright information
********************************************/

#include "devices.h"

#include "snmp.h"
#include "utils.h"
#include "monitors.h"
#include "mappings.h"
#include "settings.h"

void do_properties_recache(DeviceInfo info, MYSQL *mysql)
{
	string query = string("SELECT id, test_type, test_id, test_params, name FROM dev_props WHERE dev_type_id = " + inttostr(info.device_type));

	MYSQL_RES *mysql_res = db_query(mysql, &info, query);
	MYSQL_ROW mysql_row;

	for (uint i = 0; i < mysql_num_rows(mysql_res); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);

		string value;
		info.property_id	= strtoint(mysql_row[0]);
		info.test_type		= strtoint(mysql_row[1]);
		info.test_id		= strtoint(mysql_row[2]);
		info.test_params	= mysql_row[3];
		info.parameters.push_front(ValuePair("parameters", info.test_params));

		switch (info.test_type)
		{
			case  1:	value = process_script_monitor(info, mysql);
						value = strstripnl(value);
						break;
	
			case  2:	value = process_snmp_monitor(info, mysql);
						value = remove_surrounding_quotes(value);	
						break;
	
			case  3:	value = process_sql_monitor(info, mysql);
						break;
	
			case  4:	value = process_internal_monitor(info, mysql);
						break;
	
			default:	debuglogger(DEBUG_PROPERTY, LEVEL_WARNING, &info, "Unknown test type (" + inttostr(info.test_type) + ").");
						value = "U";
		} // end switch

		if (value != "U")
		{
			debuglogger(DEBUG_PROPERTY, LEVEL_INFO, &info, string("Device Property '") + mysql_row[4] + "' set to {'" + value + "'}");
			db_update(mysql, &info, "REPLACE INTO dev_prop_vals SET dev_id=" + inttostr(info.device_id) + ", prop_id=" + mysql_row[0] + ", value='" + db_escape(value) + "'");
		}
		else
		{
			debuglogger(DEBUG_PROPERTY, LEVEL_INFO, &info, "Device Property test failed, skipping update.");
		}
	}
	
	mysql_free_result(mysql_res);

} // end do_properties_recache

uint process_sub_device(DeviceInfo info, MYSQL *mysql)
{
	MYSQL_RES	*mysql_res;
	MYSQL_ROW	mysql_row;
	uint		status = 0;
	int			subdev_status = 0;

	debuglogger(DEBUG_SUBDEVICE, LEVEL_INFO, &info, "Starting Subdevice.");

	// setup subdevice specific parameters
	info.parameters.push_front(ValuePair("subdev_name", info.subdevice_name));

	// create an array containing the parameters for the subdevice

	string query =
		string("SELECT name, value FROM sub_dev_variables WHERE type = 'static' AND sub_dev_id = ") +
		inttostr(info.subdevice_id);

	mysql_res = db_query(mysql, &info, query);

	for (uint i = 0; i < mysql_num_rows(mysql_res); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);
		info.parameters.push_front(ValuePair(mysql_row[0], mysql_row[1]));
	}

	mysql_free_result(mysql_res);

	// depending on the subdevice type, assign values to more parameters
	switch (info.subdevice_type)
	{
		case 1:			break; // group

		case 2:			subdev_status = setup_interface_parameters(&info, mysql);
						break; // interface

		case 3:			subdev_status = setup_disk_parameters(&info, mysql);
						break; // disk

		default:		debuglogger(DEBUG_SUBDEVICE, LEVEL_WARNING, &info, "Unknown subdevice type (" +
						inttostr(info.subdevice_type) + ")");
						subdev_status = -3;

	}  // end subdevice type switch

	// delete the old dynamic entries from the cache
	db_update(mysql, &info, "DELETE FROM sub_dev_variables WHERE type = 'dynamic' AND sub_dev_id = "
		+ inttostr(info.subdevice_id));

	// insert the new dynamic entries
	for (list<ValuePair>::iterator current = info.parameters.begin(); current != info.parameters.end(); current++)
	{
		db_update(mysql, &info, "INSERT DELAYED INTO sub_dev_variables SET type = 'dynamic', sub_dev_id = "
			+ inttostr(info.subdevice_id) + ", name = '" + current->name + "', value = '" + db_escape(current->value)
			+ "'");
	}

	if (subdev_status < 0)
	{
		debuglogger(DEBUG_SUBDEVICE, LEVEL_WARNING, &info, "Subdevice aborted due to previous errors.");
		return 0;
	}

	// query the monitors for this subdevice

	query =
		string("SELECT ")						+
		string("monitors.data_type, ")			+ // 0
		string("data_types.rrd_type, ")			+ // 1
		string("monitors.min_val, ")			+ // 2
		string("monitors.max_val, ")			+ // 3
		string("monitors.tuned, ")				+ // 4
		string("monitors.test_type, ")			+ // 5
		string("monitors.test_id, ") 			+ // 6
		string("monitors.test_params, ")		+ // 7
		string("monitors.last_val, ")			+ // 8
		string("monitors.id, ")					+ // 9
		string("NOW() - monitors.last_time ")	+ // 10
		string("FROM monitors ")				+
		string("LEFT JOIN data_types ON monitors.data_type=data_types.id ") +
		string("WHERE sub_dev_id = ") + inttostr(info.subdevice_id);


	mysql_res = db_query(mysql, &info, query);

	for (uint i = 0; i < mysql_num_rows(mysql_res); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);

		info.monitor_id		= strtoint(mysql_row[9]);
		info.test_type  	= strtoint(mysql_row[5]);
		info.test_id		= strtoint(mysql_row[6]);
		info.test_params	= mysql_row[7];

		if (mysql_row[8] != NULL)
		{
			info.last_val = mysql_row[8];
		}

		if (mysql_row[10] != NULL)
		{
			info.delta_time = strtoint(mysql_row[10]);
		}

		RRDInfo rrd;

		// if we're using RRD
		if (strtoint(mysql_row[0]) > 0)
		{
			if (mysql_row[1] != NULL)
			{
				rrd.data_type   = mysql_row[1];
			}
			if (mysql_row[2] != NULL)
			{
				rrd.min_val	= mysql_row[2];
			}
			if (mysql_row[3] != NULL)
			{
				rrd.max_val	= mysql_row[3];
			}

			rrd.tuned		= strtoint(mysql_row[4]);
		} // end using rrd

		// process each monitor
		status = worstof(status, process_monitor(info, mysql, rrd));

	} // end for each monitor

	mysql_free_result(mysql_res);
	db_update(mysql, &info, "UPDATE sub_devices SET status=" + inttostr(status) + " WHERE id=" + inttostr(info.subdevice_id));
	return status;

} // end process subdevice


uint process_sub_devices(DeviceInfo info, MYSQL *mysql)
{

	MYSQL_RES	*mysql_res;
	MYSQL_ROW	mysql_row;
	uint		status = 0;

	string query = string("SELECT id, type, name FROM sub_devices WHERE dev_id=") + inttostr(info.device_id);

	mysql_res = db_query(mysql, &info, query);

	for (uint i = 0; i < mysql_num_rows(mysql_res); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);

		// setup subdevice variables
		info.subdevice_id 	= strtoint(mysql_row[0]);
		info.subdevice_type	= strtoint(mysql_row[1]);
		info.subdevice_name = mysql_row[2];

		status = worstof(status, process_sub_device(info, mysql));
	}

	mysql_free_result(mysql_res);

	return status;
}

void process_device(int dev_id)
{
	MYSQL 		mysql;
	MYSQL_RES 	*mysql_res;
	MYSQL_ROW 	mysql_row;
	uint		status = 0;
	time_t		start_time = time( NULL );
	DeviceInfo info;

	info.device_id = dev_id;

	// connect to db, get info for this device

	debuglogger(DEBUG_DEVICE, LEVEL_NOTICE, &info, "Starting device thread at " + timetostr(start_time) + ".");
	if (!db_connect(&mysql)) return;
	debuglogger(DEBUG_DEVICE, LEVEL_INFO, &info, "MySQL connection established.");
	info.mysql = (void *) &mysql;

	string query = 	string("SELECT ") 			+
			string("name, ")					+ // 0
			string("ip, ")						+ // 1
			string("snmp_version, ")			+ // 2
			string("snmp_read_community, ")		+ // 3
			string("snmp_recache_method, ")		+ // 4
			string("snmp_uptime, ")				+ // 5
			string("snmp_ifnumber, ")			+ // 6
			string("snmp_port, ")				+ // 7
			string("snmp_timeout, ")			+ // 8
			string("snmp_retries, ")			+ // 9
			string("no_snmp_uptime_check, ")	+ // 10
			string("dev_type, ")				+ // 11
			string("unknowns_on_snmp_restart ") + // 12
			string("FROM devices ")				+
			string("WHERE id=") + inttostr(dev_id);

	mysql_res = db_query(&mysql, &info, query);
	mysql_row = mysql_fetch_row(mysql_res);

	info.name					= mysql_row[0];
	info.ip						= mysql_row[1];
	info.snmp_version			= strtoint(mysql_row[2]);
	info.device_type			= strtoint(mysql_row[11]);

	debuglogger(DEBUG_DEVICE, LEVEL_INFO, &info, info.name + " / {" + info.ip + "}");

	// setup device-wide parameters
	info.parameters.push_front(ValuePair("dev_name", mysql_row[0]));
	info.parameters.push_front(ValuePair("ip", mysql_row[1]));

	// get SNMP-level info, if SNMP is used.
	if (info.snmp_version > 0)
	{
		// set SNMP parameters
		info.snmp_read_community	= mysql_row[3];
		info.snmp_port				= strtoint(mysql_row[7]);
		info.snmp_timeout			= strtoint(mysql_row[8]);
		info.snmp_retries			= strtoint(mysql_row[9]);
		int  snmp_recache_method	= strtoint(mysql_row[4]);
		int  check_snmp_uptime		= strtoint(mysql_row[10]) ? 0 : 1;
		
		// add SNMP parameters to list
		info.parameters.push_front(ValuePair("snmp_read_community", mysql_row[3]));

		// init device snmp session
		snmp_session_init(info);

		if (check_snmp_uptime)
		{
			// get uptime
			info.snmp_uptime = get_snmp_uptime(info);
			debuglogger(DEBUG_SNMP, LEVEL_INFO, &info, "SNMP Uptime is " + 
			format_time_elapsed(info.snmp_uptime / 100) + 
			+ " (" + inttostr(info.snmp_uptime) + " centiseconds)");

			// store new uptime
			db_update(&mysql, &info, "UPDATE devices SET snmp_uptime=" + inttostr(info.snmp_uptime) +
					" WHERE id=" + inttostr(dev_id));
		}
		else
		{
			debuglogger(DEBUG_DEVICE, LEVEL_WARNING, &info, "Not checking SNMP uptime as per configuration.");
			debuglogger(DEBUG_DEVICE, LEVEL_WARNING, &info, "This option should only be used as a last resort.");
		}

		if ( check_snmp_uptime && (info.snmp_uptime == 0) )
		{
			// device is snmp-dead
			info.snmp_avoid = 1;
			debuglogger(DEBUG_DEVICE, LEVEL_WARNING, &info, "Device is SNMP-dead.  Avoiding SNMP tests.");
		}
		else
		{
			if (snmp_recache_method >= 1)
			{
				// we care about SNMP agent restarts

				if (check_snmp_uptime)
				{
					if (strtoint(mysql_row[5]) == 0)
					{
						// device came back from the dead
						info.snmp_recache = 1;
						debuglogger(DEBUG_DEVICE, LEVEL_NOTICE, &info, "Device has returned from SNMP-death.");

						// write unknowns to counters, if configured to
						if (strtoint(mysql_row[12]) == 1)
						{
							info.counter_unknowns = 1;
							debuglogger(DEBUG_DEVICE, LEVEL_NOTICE, &info, "Using unknowns for non-gauge values.");
						}
					}

					if (info.snmp_uptime < strtoint(mysql_row[5]))
					{
						// uptime went backwards
						info.snmp_recache = 1;
						debuglogger(DEBUG_SNMP, LEVEL_NOTICE, &info, "SNMP Agent Restart.");

						// write unknowns to counters, if configured to
						if (strtoint(mysql_row[12]) == 1)
						{
							info.counter_unknowns = 1;
							debuglogger(DEBUG_DEVICE, LEVEL_NOTICE, &info, "Using unknowns for non-gauge values.");
						}

					}
				}
			}

			if (snmp_recache_method >= 2)
			{
				// we care about ifNumber

				info.snmp_ifnumber =  strtoint(snmp_get(info, string("interfaces.ifNumber.0")));

				debuglogger(DEBUG_SNMP, LEVEL_INFO, &info,
					"Number of Interfaces is " + inttostr(info.snmp_ifnumber));

				if (info.snmp_ifnumber != strtoint(mysql_row[6]))
				{
					// ifNumber changed
					info.snmp_recache = 1;
					db_update(&mysql, &info, "UPDATE devices SET snmp_ifnumber = " +
						inttostr(info.snmp_ifnumber) + string(" WHERE id = ") +
						inttostr(dev_id));
					debuglogger(DEBUG_SNMP, LEVEL_NOTICE, &info,
						"Number of interfaces changed from " + string(mysql_row[6]) + " to " + inttostr(info.snmp_ifnumber));
				}
				else
				if (snmp_recache_method >= 3)
				{
					// we care about interface cache matching ifNumber
					MYSQL_RES 	*cache_mysql_res;
					MYSQL_ROW 	cache_mysql_row;

					cache_mysql_res = db_query(&mysql, &info, string("SELECT count(*) FROM snmp_interface_cache WHERE dev_id = ") + inttostr(info.device_id));
					cache_mysql_row = mysql_fetch_row(cache_mysql_res);
					unsigned int interface_cache_count = strtoint(cache_mysql_row[0]);
					mysql_free_result(cache_mysql_res);

					if (info.snmp_ifnumber != interface_cache_count)
					{
						// ifNumber doesn't match the interface cache in the database
						info.snmp_recache = 1;
						debuglogger(DEBUG_SNMP, LEVEL_NOTICE, &info,
							"Number of cached interfaces (" + inttostr(interface_cache_count) + ") " +
							"doesn't match reported number of interfaces.");
					}
				}
			}

			if (strtoint(mysql_row[4]) == 4)
			{
				// we recache this one every time.
				info.snmp_recache = 1;
			}
		} // end snmp_uptime > 0

		if (info.snmp_recache)
		{
			// we need to recache.
			debuglogger(DEBUG_SNMP, LEVEL_NOTICE, &info, "Performing SNMP Recache.");
			do_snmp_interface_recache(&info, &mysql);
			do_snmp_disk_recache(&info, &mysql);
			do_properties_recache(info, &mysql);
		}
	} // end snmp-enabled
	else
	{
		debuglogger(DEBUG_SNMP, LEVEL_INFO, &info, "This device is configured with no SNMP support; avoiding SNMP tests.");
		info.snmp_avoid = 1;
	}

	mysql_free_result(mysql_res);

	// process sub-devices
	status = process_sub_devices(info, &mysql);

	db_update(&mysql, &info, "UPDATE devices SET status=" + inttostr(status) + 
		", snmp_avoided=" + inttostr(info.snmp_avoid) + " WHERE id=" + inttostr(dev_id));

	if (info.snmp_sess_p)
	{
		snmp_session_cleanup(info);
	}

	// trim event log for device
	debuglogger(DEBUG_DEVICE, LEVEL_INFO, &info, "Trimming device event log.");
	mysql_res = db_query(&mysql, &info, "SELECT id FROM log WHERE dev_id=" + inttostr(info.device_id) + " ORDER BY id");
	int count = mysql_num_rows(mysql_res);
	for (int i = 0; i < count - get_setting_int(setMaxDeviceLogEntries); i++)
	{
		mysql_row = mysql_fetch_row(mysql_res);
		db_update(&mysql, &info, string("DELETE FROM log WHERE id=") + mysql_row[0]);
	}
	mysql_free_result(mysql_res);

	mysql_close(&mysql);
	info.mysql = NULL;

	debuglogger(DEBUG_DEVICE, LEVEL_NOTICE, &info, "Ending device thread at " + timetostr(time( NULL )) + ", after a duration of " + inttostr(time(NULL) - start_time) + " seconds.");

} // end process_device