File: mappings.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 (434 lines) | stat: -rw-r--r-- 12,941 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
/********************************************
* NetMRG Integrator
*
* mappings.cpp
* NetMRG Gatherer Mappings Library
*
* see doc/LICENSE for copyright information
********************************************/

#include "mappings.h"
#include "utils.h"
#include "snmp.h"
#include "db.h"

string hex_to_dec(const string & hex)
{
	unsigned int value;
	sscanf(hex.c_str(), "%x", &value);
	return inttostr(value);
}

string cdpaddress_to_ip(const string & cdpip)
{
	string temp = cdpip;
	string ip   = "";
	int pos;
	while ((pos = temp.find(" ")) != string::npos)
	{
		ip += hex_to_dec(temp.substr(0,pos)) + ".";
		temp.erase(0, pos+1);
	}
	ip += hex_to_dec(temp);
	return ip;
}

string get_cdp_index(DeviceInfo *info, string portifindex)
{
	list<SNMPPair> cdpAddressType = snmp_walk(*info, ".1.3.6.1.4.1.9.9.23.1.2.1.1.3." + portifindex);
	cdpAddressType = snmp_trim_rootoid(cdpAddressType, ".1.3.6.1.4.1.9.9.23.1.2.1.1.3.");
	if (!cdpAddressType.empty())
		return cdpAddressType.begin()->oid;
	return "U";
}

void do_snmp_interface_recache(DeviceInfo *info, MYSQL *mysql)
{
	// clear cache for this device
	db_update(mysql, info, "DELETE FROM snmp_interface_cache WHERE dev_id=" + inttostr(info->device_id));

	// this is a hack to see if we're on a CatOS platform
	string sysdescr = snmp_get(*info, "system.sysDescr.0");

	IfMIBType mibtype = imtStandard;

	if (sysdescr.find("FSM726 Managed Switch") != string::npos)
		mibtype = imtFSM726;

	if (sysdescr.find("WS-C") != string::npos)
		mibtype = imtCatOS;

	if (sysdescr.find("Cisco Systems Catalyst 1900") != string::npos)
		mibtype = imtOldCiscoSwitch;

	// check for CDP capability
	bool cdp_enabled = (snmp_get(*info, ".1.3.6.1.4.1.9.9.23.1.3.1.0") == "1");

	list<SNMPPair> ifIndexList = snmp_walk(*info, "ifIndex");

	for (list<SNMPPair>::iterator current = ifIndexList.begin(); current != ifIndexList.end(); current++)
	{
		string ifIndex = current->value;
		string ifName  = snmp_get(*info, "ifName."  + ifIndex);

		if ((mibtype == imtStandard) &&
			(sysdescr.find("Cisco") != string::npos) &&
			(sysdescr.find("IOS") != string::npos) &&
			(ifName == "U")
		   )
		{
			mibtype = imtOldCiscoRouter;
		}
		
		string ifDescr = snmp_get(*info, "ifDescr." + ifIndex);
		// use CatOS port name in place of ifAlias
		string ifAlias;
		if (mibtype == imtCatOS)
		{
			// CatOS port names are indexed by slot and port, not by ifIndex
			string::size_type slash_pos = ifName.find("/");
			int slot = strtoint(ifName.substr(0, slash_pos));
			int port = strtoint(ifName.substr(slash_pos +  1, ifName.length() - 1));
			debuglogger(DEBUG_SNMP, LEVEL_DEBUG, info, "ifname='" + ifName + "', slash_pos=" + inttostr(slash_pos) + ", slot=" + inttostr(slot) + ", port=" + inttostr(port));
			if ( (slot != 0) && (port != 0) )
			{
				ifAlias = snmp_get(*info, ".1.3.6.1.4.1.9.5.1.4.1.1.4." + inttostr(slot) + "." + inttostr(port));
				ifAlias.erase(0, 1);
				ifAlias.erase(ifAlias.length() - 1, 1);
			}
		}
		else if (mibtype == imtFSM726)
		{
			ifAlias = snmp_get(*info, ".1.3.6.1.4.1.4526.1.4.11.6.1.13." + ifIndex);
			ifAlias = remove_surrounding_quotes(ifAlias);
			ifName  = ifDescr;	
		}
		else if (mibtype == imtOldCiscoSwitch)
		{
			ifAlias = snmp_get(*info, ".1.3.6.1.4.1.437.1.1.3.3.1.1.3." + ifIndex);
			ifAlias = remove_surrounding_quotes(ifAlias);
			ifName  = ifDescr;	
		}
		else if (mibtype == imtOldCiscoRouter)
		{
			ifAlias = snmp_get(*info, ".1.3.6.1.4.1.9.2.2.1.1.28." + ifIndex);
			ifAlias = remove_surrounding_quotes(ifAlias);
			ifName  = ifDescr;
		}
		else
		{
			ifAlias = snmp_get(*info, "ifAlias." + ifIndex);
		}
		U_to_NULL(ifAlias);
		U_to_NULL(ifName);
		U_to_NULL(ifDescr);
		string ifType			= snmp_get(*info, "ifType."  + ifIndex);
		string ifMAC			= snmp_get(*info, "ifPhysAddress." + ifIndex);
		U_to_NULL(ifMAC);
		string ifOperStatus		= snmp_get(*info, "ifOperStatus."  + ifIndex);
		string ifAdminStatus	= snmp_get(*info, "ifAdminStatus." + ifIndex);
		string ifSpeed			= snmp_get(*info, "ifSpeed." + ifIndex);
		string vlan				= snmp_get(*info, ".1.3.6.1.4.1.9.9.68.1.2.2.1.2." + ifIndex);
		U_to_NULL(vlan);

		// CDP Next Hop
		string nexthop = "U";
		if (cdp_enabled)
		{
			string cdp_index = get_cdp_index(info, ifIndex);
			if (cdp_index != "U")
			{
				string cdp_port = remove_surrounding_quotes(snmp_get(*info, ".1.3.6.1.4.1.9.9.23.1.2.1.1.7." + cdp_index));
				string raw_cdp_ip = snmp_get(*info, ".1.3.6.1.4.1.9.9.23.1.2.1.1.4." + cdp_index);
				string cdp_ip = cdpaddress_to_ip(raw_cdp_ip);
	
				MYSQL_RES 	*nh_mysql_res;
				MYSQL_ROW 	nh_mysql_row;
				nh_mysql_res = db_query(mysql, info, string(
					"SELECT sd.id FROM devices d, sub_devices sd, sub_dev_variables sdv ") + 
					"WHERE d.ip = '" + cdp_ip + "'" +
					"AND sdv.name = 'ifDescr' AND sdv.value = '" + cdp_port + "'" +
					"AND d.id = sd.dev_id AND sd.id = sdv.sub_dev_id");
				if (nh_mysql_row = mysql_fetch_row(nh_mysql_res))
				{
					nexthop = string(nh_mysql_row[0]);
				}
				mysql_free_result(nh_mysql_res);
			}
		}
		U_to_NULL(nexthop);

		db_update(mysql, info, string("INSERT INTO snmp_interface_cache SET ")  +
			"dev_id = " 		+ inttostr((*info).device_id)		+ ", "  +
			"ifIndex = '"		+ ifIndex							+ "', " +
			"ifName = "			+ ifName							+ ", "  +
			"ifDescr = "		+ ifDescr							+ ", "  +
			"ifAlias = "		+ ifAlias							+ ", "  +
			"ifType = '"		+ ifType							+ "', " +
			"ifMAC = "			+ ifMAC								+ ", "  +
			"ifOperStatus = '" 	+ ifOperStatus						+ "', " +
			"ifAdminStatus = '" + ifAdminStatus						+ "', " +
			"ifSpeed = '"		+ ifSpeed							+ "', " +
			"vlan = "			+ vlan								+ ", "  +
			"nexthop = "		+ nexthop							+ "");
	}

	list<SNMPPair> ifIPList = snmp_walk(*info, "ipAdEntIfIndex");
	ifIPList = snmp_trim_rootoid(ifIPList, ".1.3.6.1.2.1.4.20.1.2.");

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

		db_update(mysql, info, string("UPDATE snmp_interface_cache SET ifIP = '") +
			ip + "' WHERE dev_id=" + inttostr((*info).device_id) +
			" AND ifIndex=" + ifIndex);
	}
}

void do_snmp_disk_recache(DeviceInfo *info, MYSQL *mysql)
{
	// clear cache for this device
	db_update(mysql, info, "DELETE FROM snmp_disk_cache WHERE dev_id=" + inttostr((*info).device_id));

	// try UCD Disk MIB
	
	list<SNMPPair> dskIndexList = snmp_walk(*info, "dskIndex");

	for (list<SNMPPair>::iterator current = dskIndexList.begin(); current != dskIndexList.end(); current++)
	{
		string dskIndex  = current->value;
		string dskPath   = snmp_get(*info, "dskPath."   + dskIndex);	U_to_NULL(dskPath);
		string dskDevice = snmp_get(*info, "dskDevice." + dskIndex);	U_to_NULL(dskDevice);

		db_update(mysql, info, string("INSERT INTO snmp_disk_cache SET ")  +
			"dev_id = " 		+ inttostr((*info).device_id) 	+ ", "  +
			"disk_index  = "	+ dskIndex 			+ ", "  +
			"disk_device = "	+ dskDevice			+ ", " +
			"disk_path   = "	+ dskPath);
	}
	
	// try Windows Disk MIB
	
	if (dskIndexList.empty())
	{
		string dskPath, dskIndex;
		dskIndexList = snmp_walk(*info, ".1.3.6.1.2.1.25.2.3.1.1");
		for (list<SNMPPair>::iterator current = dskIndexList.begin(); current != dskIndexList.end(); current++)
		{
			dskIndex  = current->value;
			dskPath   = snmp_get(*info, ".1.3.6.1.2.1.25.2.3.1.3." + dskIndex); U_to_NULL(dskPath);
			if (dskPath[1] == '\\' && dskPath[2] == '"' &&
			    dskPath[dskPath.size()-3] == '\\' && dskPath[dskPath.size()-2] == '"')
				{
					dskPath.erase(1, 2);
					dskPath.erase(dskPath.size()-3, 2);
				}
			string::size_type i = dskPath.find(" ", 0);
			if (i != string::npos)
			{
				dskPath = dskPath.substr(0, i+1) + "'";
			}
			db_update(mysql, info, string("INSERT INTO snmp_disk_cache SET ")	+
				"dev_id = "             + inttostr((*info).device_id)			+ ", "  +
				"disk_index  = "        + dskIndex								+ ", "  +
				"disk_device = "        + dskPath								+ ", "  +
				"disk_path   = "        + dskPath);
		}
	}

}

int setup_interface_parameters(DeviceInfo *info, MYSQL *mysql)
{

	// This function examines the parameters for the subdevice and determines if any
	// are to be used as SNMP index values.  If so, it adds parameters with all available
	// information from the snmp_cache, so that things like %ifIndex% and %ifName% in monitors
	// will get expanded into the correct values when the monitors are processed.

	string          index   = "";
	string          value   = "";

	MYSQL_RES       *mysql_res;
	MYSQL_ROW       mysql_row;

	int		retval	= 0;

	for (list<ValuePair>::iterator current = info->parameters.begin(); current != info->parameters.end(); current++)
	{
		value = current->value;

		if (
				current->name == "ifIndex" ||
				current->name == "ifName" ||
				current->name == "ifDescr" ||
				current->name == "ifAlias" ||
				current->name == "ifIP" ||
				current->name == "ifMAC"
			)
		{
			index = current->name;
                	break;
		}

	} // end for each parameter

	if (index == "")
	{
		debuglogger(DEBUG_SUBDEVICE, LEVEL_WARNING, info, "Interface subdevice has no interface parameters.");
		retval = -1;
	}
	else
	{
		string query =
			string("SELECT ifIndex, ifName, ifIP, ifDescr, ifAlias, ifMAC, ifSpeed, nexthop FROM snmp_interface_cache WHERE dev_id=") +
			inttostr(info->device_id) + string(" AND ") + index + "='" + db_escape(value) + "'";

		mysql_res = db_query(mysql, info, query);

		if (mysql_num_rows(mysql_res) > 0)
		{
			mysql_row = mysql_fetch_row(mysql_res);

			if ((mysql_row[0] != NULL) && (index != "ifIndex"))
			{
				info->parameters.push_front(ValuePair("ifIndex", mysql_row[0]));
			}

			if ((mysql_row[1] != NULL) && (index != "ifName"))
			{
				info->parameters.push_front(ValuePair("ifName", mysql_row[1]));
			}

			if ((mysql_row[2] != NULL) && (index != "ifIP"))
			{
				info->parameters.push_front(ValuePair("ifIP", mysql_row[2]));
			}

			if ((mysql_row[3] != NULL) && (index != "ifDescr"))
			{
				info->parameters.push_front(ValuePair("ifDescr", mysql_row[3]));
			}

			if ((mysql_row[4] != NULL) && (index != "ifAlias"))
			{
				info->parameters.push_front(ValuePair("ifAlias", mysql_row[4]));
				parse_fancy_alias(info, mysql_row[4]);
			}

			if ((mysql_row[5] != NULL) && (index != "ifMAC"))
			{
				info->parameters.push_front(ValuePair("ifMAC", mysql_row[5]));
			}

			if (mysql_row[6] != NULL)
				info->parameters.push_front(ValuePair("ifSpeed", mysql_row[6]));

			if (mysql_row[7] != NULL)
				info->parameters.push_front(ValuePair("nexthop", mysql_row[7]));
		}
		else
		{
			debuglogger(DEBUG_SUBDEVICE, LEVEL_WARNING, info, "Interface index not found.");
			retval = -2;
		}
	 	mysql_free_result(mysql_res);
	}
	return retval;
}

void parse_fancy_alias(DeviceInfo *info, string alias)
{
	// see if the interface description looks parsible, and parse it.
	if (alias.find("(",0) != string::npos)
	{
		info->parameters.push_front(ValuePair("ifCktName", alias.substr(0, alias.find("(",0))));
		info->parameters.push_front(ValuePair("ifCktID", alias.substr(alias.find("(",0) + 1, alias.length() - alias.find("(",0) - 2)));
	}
	else
	{
		info->parameters.push_front(ValuePair("ifCktName", alias));
		info->parameters.push_front(ValuePair("ifCktID", "N/A"));
	}
}

int setup_disk_parameters(DeviceInfo *info, MYSQL *mysql)
{
	// just like setup_interface_parameters, but for disks instead

	string		index   = "";
	string		value   = "";

	MYSQL_RES	*mysql_res;
	MYSQL_ROW	mysql_row;

	int		retval	= 0;

	for (list<ValuePair>::iterator current = info->parameters.begin(); current != info->parameters.end(); current++)
	{
		value = current->value;

		if (current->name == "dskIndex")
		{
			index = "disk_index";
			break;
		}
		else
		if (current->name == "dskPath")
		{
			index = "disk_path";
			break;
		}
		else
		if (current->name == "dskDevice")
		{
			index = "disk_device";
			break;
		}

	} // end for each parameter

	if (index == "")
	{
		debuglogger(DEBUG_SUBDEVICE, LEVEL_WARNING, info, "Disk subdevice has no disk parameters.");
		retval = -1;
	}
	else
	{
		string query =
		string("SELECT disk_index, disk_path, disk_device FROM snmp_disk_cache WHERE dev_id=") +
		inttostr(info->device_id) + string(" AND ") + index + "='" + db_escape(value) + "'";

		mysql_res = db_query(mysql, info, query);

		if (mysql_num_rows(mysql_res) > 0)
		{
			mysql_row = mysql_fetch_row(mysql_res);

			if ((mysql_row[0] != NULL) && (index != "disk_index"))
			{
				info->parameters.push_front(ValuePair("dskIndex", mysql_row[0]));
			}

			if ((mysql_row[1] != NULL) && (index != "disk_path"))
			{
				info->parameters.push_front(ValuePair("dskPath", mysql_row[1]));
			}

			if ((mysql_row[2] != NULL) && (index != "disk_device"))
			{
				info->parameters.push_front(ValuePair("dskDevice", mysql_row[2]));
			}
		}
		else
		{
			debuglogger(DEBUG_SUBDEVICE, LEVEL_WARNING, info, "Disk index not found.");
			retval = -2;
		}
	 	mysql_free_result(mysql_res);
	}
	return retval;
}