[go: up one dir, main page]

File: netsnmp.c

package info (click to toggle)
scli 0.3.1-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,872 kB
  • ctags: 8,627
  • sloc: ansic: 32,657; sh: 7,388; makefile: 321
file content (447 lines) | stat: -rw-r--r-- 10,620 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* 
 * netsnmp.c -- scli ucdsnmp/netsnmp mode implementation
 *
 * Copyright (C) 2002 Juergen Schoenwaelder
 *
 * 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, 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 * @(#) $Id: netsnmp.c 717 2004-10-14 21:16:08Z schoenw $
 */

#include "scli.h"

#include "ucd-snmp-mib.h"

#define UCD_SNMP_MIB_VERSIONDODEBUGGING_DISABLED 0
#define UCD_SNMP_MIB_VERSIONDODEBUGGING_ENABLED  1

static GNetSnmpEnum const ucd_snmp_mib_enums_versionDoDebugging[] = {
    { UCD_SNMP_MIB_VERSIONDODEBUGGING_DISABLED, "disabled" },
    { UCD_SNMP_MIB_VERSIONDODEBUGGING_ENABLED,  "enabled" },
    { 0, NULL }
};


static void
fmt_info(GString *s, ucd_snmp_mib_version_t *version)
{
    int const indent = 18;
    const char *e;

    if (version->versionTag) {
	fmt_display_string(s, indent, "Version:",
			   (int) version->_versionTagLength,
			   version->versionTag);
    }
    if (version->versionConfigureOptions) {
	fmt_display_string(s, indent, "Configuration:",
			   (int) version->_versionConfigureOptionsLength,
			   version->versionConfigureOptions);
    }

    e = fmt_enum(ucd_snmp_mib_enums_versionDoDebugging,
		 version->versionDoDebugging);
    if (e) {
	g_string_sprintfa(s, "%-*s%s\n", indent, "Debugging:", e);
    }
}



static int
show_netsnmp_info(scli_interp_t *interp, int argc, char **argv)
{
    ucd_snmp_mib_version_t *version = NULL;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    if (scli_interp_dry(interp)) {
	return SCLI_OK;
    }

    ucd_snmp_mib_get_version(interp->peer, &version,
			     UCD_SNMP_MIB_VERSIONTAG
			     | UCD_SNMP_MIB_VERSIONCONFIGUREOPTIONS
			     | UCD_SNMP_MIB_VERSIONDODEBUGGING);
    if (interp->peer->error_status) {
	return SCLI_SNMP;
    }

    if (version) {
	fmt_info(interp->result, version);
    }

    if (version) ucd_snmp_mib_free_version(version);

    return SCLI_OK;
}



static void
fmt_load(GString *s, ucd_snmp_mib_laEntry_t *laEntry)
{
    const char *names[] = {
	"Load-1", "Load-5", "Load-15", NULL
    };
    const char *labels[] = {
	"Load (1 min):", "Load (5 min):", "Load (15 min):", NULL
    };
    int const indent = 18; 
    int i;

    for (i = 0; names[i]; i++) {
	if (strncmp(laEntry->laNames, names[i],
		    laEntry->_laNamesLength) == 0) {
	    break;
	}
    }
    if (! names[i]) {
	return;
    }

    g_string_sprintfa(s, "%-*s%.2f\n", indent, labels[i],
		      *laEntry->laLoadInt / 100.0);

    if (laEntry->_laErrMessageLength) {
	fmt_display_string(s, indent, "Error:",
			   (int) laEntry->_laErrMessageLength,
			   laEntry->laErrMessage);
    }
}



static int
show_netsnmp_load(scli_interp_t *interp, int argc, char **argv)
{
    ucd_snmp_mib_laEntry_t **laTable = NULL;
    int i;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    if (scli_interp_dry(interp)) {
	return SCLI_OK;
    }

    ucd_snmp_mib_get_laTable(interp->peer, &laTable,
			     UCD_SNMP_MIB_LANAMES
			     | UCD_SNMP_MIB_LALOADINT
			     | UCD_SNMP_MIB_LAERRMESSAGE);
    if (interp->peer->error_status) {
	return SCLI_SNMP;
    }
    
    if (laTable) {
	for (i = 0; laTable[i]; i++) {
	    fmt_load(interp->result, laTable[i]);
	}
    }
    
    if (laTable) ucd_snmp_mib_free_laTable(laTable);

    return SCLI_OK;
}



static void
fmt_ext(GString *s, ucd_snmp_mib_extEntry_t *extEntry)
{
    int const indent = 18;

    g_string_sprintfa(s, "%-*s%d\n", indent, "Index:",
		      extEntry->extIndex);
    fmt_display_string(s, indent, "Name:",
		       (int) extEntry->_extNamesLength,
		       extEntry->extNames);
    fmt_display_string(s, indent, "Command:",
		       (int) extEntry->_extCommandLength,
		       extEntry->extCommand);
    fmt_display_string(s, indent, "Output:",
		       (int) extEntry->_extOutputLength,
		       extEntry->extOutput);
    if (extEntry->extResult) {
	g_string_sprintfa(s, "%-*s%d\n", indent, "Code:",
			  *extEntry->extResult);
    }
}



static int
show_netsnmp_exec(scli_interp_t *interp, int argc, char **argv)
{
    ucd_snmp_mib_extEntry_t **extTable = NULL;
    int i;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    if (scli_interp_dry(interp)) {
	return SCLI_OK;
    }

    ucd_snmp_mib_get_extTable(interp->peer, &extTable, 0);
    if (interp->peer->error_status) {
	return SCLI_SNMP;
    }

    if (extTable) {
	for (i = 0; extTable[i]; i++) {
	    fmt_ext(interp->result, extTable[i]);
	}
    }

    if (extTable) ucd_snmp_mib_free_extTable(extTable);

    return SCLI_OK;
}



static void
fmt_proc(GString *s, ucd_snmp_mib_prEntry_t *prEntry)
{
    int const indent = 18;

    g_string_sprintfa(s, "%-*s%d\n", indent, "Index:",
		      prEntry->prIndex);
    fmt_display_string(s, indent, "Process:",
		       (int) prEntry->_prNamesLength,
		       prEntry->prNames);
    g_string_sprintfa(s, "%-*s%d\n", indent, "Minimum:",
		      *prEntry->prMin);
    g_string_sprintfa(s, "%-*s%d\n", indent, "Maximum:",
		      *prEntry->prMax);
    g_string_sprintfa(s, "%-*s%d\n", indent, "Current:",
		      *prEntry->prCount);
    if (prEntry->_prErrMessageLength) {
	fmt_display_string(s, indent, "Error:",
			   (int) prEntry->_prErrMessageLength,
			   prEntry->prErrMessage);
    }
}



static int
show_netsnmp_proc(scli_interp_t *interp, int argc, char **argv)
{
    ucd_snmp_mib_prEntry_t **prTable = NULL;
    int i;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    if (scli_interp_dry(interp)) {
	return SCLI_OK;
    }

    ucd_snmp_mib_get_prTable(interp->peer, &prTable, 0);
    if (interp->peer->error_status) {
	return SCLI_SNMP;
    }

    if (prTable) {
	for (i = 0; prTable[i]; i++) {
	    if (i) g_string_append(interp->result, "\n");
	    fmt_proc(interp->result, prTable[i]);
	}
    }

    if (prTable) ucd_snmp_mib_free_prTable(prTable);

    return SCLI_OK;
}



static int
set_netsnmp_debugging(scli_interp_t *interp, int argc, char **argv)
{
    gint32 value = 1;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 2) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (! gnet_snmp_enum_get_number(ucd_snmp_mib_enums_versionDoDebugging,
				    argv[1], &value)) {
	g_string_assign(interp->result, argv[1]);
	return SCLI_SYNTAX_VALUE;
    }

    if (scli_interp_dry(interp)) {
	return SCLI_OK;
    }

    ucd_snmp_mib_set_versionDoDebugging(interp->peer, value);
    if (interp->peer->error_status) {
	return SCLI_SNMP;
    }

    return SCLI_OK;
}



static int
set_netsnmp_restart(scli_interp_t *interp, int argc, char **argv)
{
    gint32 value = 1;

    g_return_val_if_fail(interp, SCLI_ERROR);
    
    if (argc != 1) {
	return SCLI_SYNTAX_NUMARGS;
    }

    if (scli_interp_dry(interp)) {
	return SCLI_OK;
    }

    ucd_snmp_mib_set_versionRestartAgent(interp->peer, value);
    if (interp->peer->error_status) {
	return SCLI_SNMP;
    }

    return SCLI_OK;
}



static int
dump_netsnmp(scli_interp_t *interp, int argc, char **argv)
{
    ucd_snmp_mib_version_t *version = NULL;
    const char *e;

    g_return_val_if_fail(interp, SCLI_ERROR);

    if (argc > 1) {
	return SCLI_SYNTAX_NUMARGS;
    }
    
    if (scli_interp_dry(interp)) {
	return SCLI_OK;
    }

    ucd_snmp_mib_get_version(interp->peer, &version,
			     UCD_SNMP_MIB_VERSIONDODEBUGGING);
    if (interp->peer->error_status) {
	return SCLI_SNMP;
    }

    if (version) {
	e = fmt_enum(ucd_snmp_mib_enums_versionDoDebugging,
		     version->versionDoDebugging);
	if (e) {
	    g_string_sprintfa(interp->result, "set netsnmp debugging %s\n", e);
	}
    }

    if (version) ucd_snmp_mib_free_version(version);
    
    return SCLI_OK;
}



void
scli_init_netsnmp_mode(scli_interp_t *interp)
{
    static scli_cmd_t cmds[] = {

	{ "set netsnmp debugging", "<value>",
	  "The `set netsnmp debugging' command controls whether the agent\n"
	  "generates debug messages or not. The <value> parameter must\n"
	  "be one of the strings \"enabled\" or \"disabled\".",
	  SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY | SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  set_netsnmp_debugging },
	
	{ "set netsnmp restart", NULL,
	  "The `set netsnmp restart' command restarts the agent.",
	  SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY | SCLI_CMD_FLAG_NORECURSE,
	  NULL, NULL,
	  set_netsnmp_restart },
	
	{ "show netsnmp info", NULL,
	  "The `show netsnmp info' command shows general information about\n"
	  "the netsnmp/ucdsnmp agent such as the version number and the\n"
	  "software configuration.",
	  SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
	  NULL, NULL,
	  show_netsnmp_info },

	{ "show netsnmp load", NULL,
	  "The `show netsnmp load' command shows the load indices of the\n"
	  "system. This is usually the length of the queue in front of\n"
	  "the processor(s) averaged over some time interval.",
	  SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
	  NULL, NULL,
	  show_netsnmp_load },

	{ "show netsnmp exec", NULL,
	  "The `show netsnmp exec' command shows information about\n"
	  "pre-configured commands that can be invoked.",
	  SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
	  NULL, NULL,
	  show_netsnmp_exec },

	{ "show netsnmp proc", NULL,
	  "The `show netsnmp proc' command shows information about\n"
	  "which processes netsnmp watches.",
	  SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
	  NULL, NULL,
	  show_netsnmp_proc },

	{ "dump netsnmp", NULL,
	  "The `dump netsnmp' command generates a sequence of scli commands\n"
	  "which can be used to restore the netsnmp specific configuration.\n",
	  SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
	  NULL, NULL,
	  dump_netsnmp },

	{ NULL, NULL, NULL, 0, NULL, NULL, NULL }
    };
    
    static scli_mode_t netsnmp_mode = {
	"netsnmp",
	"The netsnmp scli mode is used to display and configure netsnmp\n"
	"specific parameters. It is based on the UCD-SNMP-MIB.",
	cmds
    };

    scli_register_mode(interp, &netsnmp_mode);
}