File: snmp.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 (402 lines) | stat: -rw-r--r-- 9,758 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
/********************************************
* NetMRG Integrator
*
* snmp.cpp
* NetMRG Gatherer SNMP Library
*
* see doc/LICENSE for copyright information
********************************************/

/*

   NetMRG SNMP Functions
   Copyright 2001-2003 Brady Alleman, All Rights Reserved.

   Some of this code was originally part of net-snmp's application and
   example code.

*/

#include "utils.h"
#include "locks.h"
#include "snmp.h"

#ifdef HAVE_NET_SNMP
#include <net-snmp-config.h>
#include <net-snmp-includes.h>
#include <config_api.h>
#include <mib_api.h>
#define SNMP_SET_OIDS netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, NETSNMP_OID_OUTPUT_NUMERIC)
#define SNMP_SET_LIBS netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, 1)
#define SNMP_SET_QKPR netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, 0)
#else
#define DS_APP_DONT_FIX_PDUS 0
#include <ucd-snmp-config.h>
#include <ucd-snmp-includes.h>
#include <system.h>
#include <mib.h>
#define SNMP_SET_OIDS ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_PRINT_NUMERIC_OIDS)
#define SNMP_SET_LIBS ds_toggle_boolean(DS_LIBRARY_ID, DS_LIB_PRINT_NUMERIC_ENUM)
#define SNMP_SET_QKPR snmp_set_quick_print(0)
#endif

void snmp_init()
{
	debuglogger(DEBUG_GLOBAL + DEBUG_SNMP, LEVEL_INFO, NULL, "Initializing SNMP library.");
	init_snmp("NetMRG");
	SOCK_STARTUP;
	struct snmp_session session;
	snmp_sess_init(&session);
	SNMP_SET_OIDS;
	SNMP_SET_LIBS;
	SNMP_SET_QKPR;
}

void snmp_cleanup()
{
	SOCK_CLEANUP;
	debuglogger(DEBUG_GLOBAL + DEBUG_SNMP, LEVEL_INFO, NULL, "Cleaned up SNMP.");
}

string snmp_value(string input)
{
	input = input.erase(0, input.find(":",0) + 1);
	input = input.erase(0, input.find("=",0) + 1);
	while (!input.empty() && input[0] == ' ')
	{
		input = input.erase(0, 1);
	}
	while (!input.empty() && input[input.length() -1] == ' ')
	{
		input = input.erase(input.length() - 1, input.length());
	}
	//input = token_replace(input, " ", "");

	// handle an "empty" SNMPv2 response.
	input = token_replace(input, "No Such Object available on this agent at this OID", "");

	return input;
}

string snmp_oid(string input)
{
	input = input.erase(input.find(" ",0), input.length());

	return input;
}

string snmp_result(variable_list *vars)
{
	u_char         *buf = NULL;
	size_t          buf_len = 256, out_len = 0;

	buf = (u_char *) calloc(buf_len, 1);
	sprint_realloc_variable(&buf, &buf_len, &out_len, 1, vars->name, vars->name_length, vars);
	string result = (char *)buf;
	free(buf);

	return result;
}

void snmp_session_init(DeviceInfo &info)
{
	struct	snmp_session session;
	u_char	u_temp[250];
	char	temp[250];
	void    * sessp;

	debuglogger(DEBUG_SNMP, LEVEL_DEBUG, &info, "Starting SNMP Session.");
	
	// initialize session structure
	snmp_sess_init(&session);

	// set hostname or IP address (and port)
	snprintf(temp, 250, "%s:%d", info.ip.c_str(), info.snmp_port);
	session.peername = temp;

	// set the SNMP version number
	switch (info.snmp_version)
	{
		case 1: session.version = SNMP_VERSION_1;
				debuglogger(DEBUG_SNMP, LEVEL_DEBUG, &info, "SNMPv1");
				break;
		case 2: session.version = SNMP_VERSION_2c;
				debuglogger(DEBUG_SNMP, LEVEL_DEBUG, &info, "SNMPv2c");
				break;
		case 3: session.version = SNMP_VERSION_3;
				debuglogger(DEBUG_SNMP, LEVEL_ERROR, &info, "SNMPv3 - not yet supported.");
				break;
	}
	
	// set timeout/retry parameters
	session.timeout = info.snmp_timeout;
	session.retries = info.snmp_retries;

	char log[255];
	snprintf(log, 255, "Port: %d; Timeout: %ld; Retries: %d.", info.snmp_port, info.snmp_timeout, info.snmp_retries);
	debuglogger(DEBUG_SNMP, LEVEL_DEBUG, &info, log);
		
	// set the SNMPv1/2c community name used for authentication
	session.community = u_string(info.snmp_read_community, u_temp);
	session.community_len = info.snmp_read_community.length();

	netmrg_mutex_lock(lkSNMP);
	sessp = snmp_sess_open(&session);
	netmrg_mutex_unlock(lkSNMP);
	
	if (!sessp)
	{
		debuglogger(DEBUG_SNMP, LEVEL_ERROR, &info, "SNMP Session Error.");
	}
	else
	{
		info.snmp_sess_p = sessp;
	}
		
}

void snmp_session_cleanup(DeviceInfo &info)
{
	debuglogger(DEBUG_SNMP, LEVEL_DEBUG, &info, "Cleaning up SNMP Session.");
	snmp_sess_close(info.snmp_sess_p);
	info.snmp_sess_p = NULL;
}

// snmp_get - perform an snmpget on a host using the provided information
string snmp_get(DeviceInfo info, string oidstring)
{
	struct 	snmp_pdu *pdu;
	struct 	snmp_pdu *response;

	oid 	anOID[MAX_OID_LEN];
	size_t 	anOID_len = MAX_OID_LEN;

	struct 	variable_list *vars;
	int 	status;
	string 	result;
	char 	tempname[128];

	debuglogger(DEBUG_SNMP, LEVEL_DEBUG, &info, "SNMP Query ({'" +
		info.ip + "'}, {'" + info.snmp_read_community + "'}, '" +
		oidstring + "')");

	if (!info.snmp_sess_p)
	{
		debuglogger(DEBUG_SNMP, LEVEL_ERROR, &info, "SNMP Session Failure.");
		return string("U");
	}
	else
	{
		// Create the PDU for the data for our request.
		pdu = snmp_pdu_create(SNMP_MSG_GET);

		strcpy(tempname, oidstring.c_str());
		if (!snmp_parse_oid(tempname, anOID, &anOID_len))
		{
			return(string("U"));
		}
		else
		snmp_add_null_var(pdu, anOID, anOID_len);

		status = snmp_sess_synch_response(info.snmp_sess_p, pdu, &response);
		
		if (response == NULL)
			status = STAT_ERROR;

		/*
		* Process the response.
		*/

		if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR)
		{
			vars = response->variables;
			if (vars->type == SNMP_NOSUCHINSTANCE || vars->type == SNMP_NOSUCHOBJECT || vars->type == SNMP_ENDOFMIBVIEW)
			{
				result = string("U");
			}
			else
			{
				result = snmp_result(vars);
				result = snmp_value(result);
			}
		}
		else
		{
			result = string("U");
		}

		if (response) snmp_free_pdu(response);

		if (result.length() == 0) { result = "U"; }

		return result;
	}
}

string snmp_diff(DeviceInfo info, string oid1, string oid2)
{
	string val1 = snmp_get(info, oid1);
	string val2 = snmp_get(info, oid2);

	if ( (val1 == "U") || (val2 == "U") )
	{
		return "U";
	}
	
	return inttostr( strtoint(val1) - strtoint(val2) );
}

list<SNMPPair> snmp_trim_rootoid(list<SNMPPair> input, string rootoid)
{
	for (list<SNMPPair>::iterator current = input.begin(); current != input.end(); current++)
	{
		current->oid = token_replace(current->oid, rootoid, "");
	}

	return input;
}

list<SNMPPair> snmp_swap_index_value(list<SNMPPair> input)
{
	for (list<SNMPPair>::iterator current = input.begin(); current != input.end(); current++)
	{
		string oid   = current->oid;
		string value = current->value;

		current->oid   = value;
		current->value = oid;
	}

	return input;
}

list<SNMPPair> snmp_walk(DeviceInfo info, string oidstring)
{
	struct snmp_pdu	*pdu, *response;
	variable_list	*vars;
	oid				name[MAX_OID_LEN];
	size_t			name_length = MAX_OID_LEN;
	oid				root[MAX_OID_LEN];
	size_t			rootlen = MAX_OID_LEN;
	int				running;
	int				status;
	int				check = 0;
	int				exitval = 0;
	list<SNMPPair>	results;

	if (!info.snmp_sess_p)
	{
		debuglogger(DEBUG_SNMP, LEVEL_ERROR, &info, "SNMP Session Failure.");
	}

	char tempoid[128];
	strcpy(tempoid, oidstring.c_str());
	if (!snmp_parse_oid(tempoid, root, &rootlen))
	{
		debuglogger(DEBUG_SNMP, LEVEL_ERROR, &info, string("SNMP OID Parse Failure (") + tempoid + ")");
	}

	memmove(name, root, rootlen * sizeof(oid));
	name_length = rootlen;

	running = 1;

	while (running)
	{
		pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
		snmp_add_null_var(pdu, name, name_length);

		status = snmp_sess_synch_response(info.snmp_sess_p, pdu, &response);
		
		if (response == NULL)
			status = STAT_ERROR;
		
		if (status == STAT_SUCCESS)
		{
			if (response->errstat == SNMP_ERR_NOERROR)
			{
				for (vars = response->variables; vars; vars = vars->next_variable)
				{
					if ((vars->name_length < rootlen) || (memcmp(root, vars->name, rootlen * sizeof(oid)) != 0))
					{
						running = 0;
						continue;
					}
					string result = snmp_result(vars);
					debuglogger(DEBUG_SNMP, LEVEL_DEBUG, &info, "OID: '" + snmp_oid(result) + "' VALUE: '" + snmp_value(result) + "'");
					results.push_front(SNMPPair(snmp_oid(result), snmp_value(result)));
					if ((vars->type != SNMP_ENDOFMIBVIEW) && (vars->type != SNMP_NOSUCHOBJECT) && (vars->type != SNMP_NOSUCHINSTANCE))
					{
						if (check && snmp_oid_compare(name, name_length, vars->name, vars->name_length) >= 0)
						{
							debuglogger(DEBUG_SNMP, LEVEL_WARNING, &info, "SNMP Error: OID not increasing");
							running = 0;
							exitval = 1;
						}
						memmove((char *) name, (char *) vars->name, vars->name_length * sizeof(oid));
						name_length = vars->name_length;
					}
					else
					{
						running = 0;
					}
				}
			}
			else
			{
				running = 0;
				if (response->errstat == SNMP_ERR_NOSUCHNAME)
				{
					debuglogger(DEBUG_SNMP, LEVEL_NOTICE, &info, "End of MIB");
				}
				else
				{
					debuglogger(DEBUG_SNMP, LEVEL_WARNING, &info, string("SNMP Packet Error: ") + snmp_errstring(response->errstat));
					exitval = 2;
				}
			}
		}
		else
		{
			if (status == STAT_TIMEOUT)
			{
				debuglogger(DEBUG_SNMP, LEVEL_WARNING, &info, string("Timeout: No Response from ") + info.ip);
				running = 0;
				exitval = 1;
			}
			else
			{
				debuglogger(DEBUG_SNMP, LEVEL_ERROR, &info, string("SNMP Walk Error (") + inttostr(status) + ")");
				running = 0;
				exitval = 1;
        	}
		}

		if (response)
			snmp_free_pdu(response);
	}

	return results;
}

long long int get_snmp_uptime(DeviceInfo info)
{
	string uptime;
	char unparsed[100];
	char * parsed;

	uptime = snmp_get(info, string("system.sysUpTime.0"));

	if (uptime != "")
	{
		strcpy(unparsed,uptime.c_str());
		parsed = strtok(unparsed, "()");
		return strtoint(string(parsed));
	}
	else
	{
		return 0;
	}

} // end get_snmp_uptime()