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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
/*
* scli.h -- emulate a command line interface via SNMP
*
* After more than 10 years of SNMP, I felt it is time for the
* first really useful command line SNMP management tool. ;-)
*
* Copyright (C) 2001 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: scli.h 2094 2007-01-02 17:15:11Z schoenw $
*/
#ifndef _SCLI_H
#define _SCLI_H
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <time.h>
#ifdef HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#include <regex.h>
#include <libxml/xmlmemory.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <glib.h>
#include <gmodule.h>
#include <gsnmp.h>
extern char const scli_copyright[]; /* copyright message (surprise) */
/*
* Some defines used internally to compute time differences and
* to check whether the polling interval was too short.
*/
#define TV_DIFF(t1, t2) (1.0 * (t2.tv_sec - t1.tv_sec) + \
(t2.tv_usec - t1.tv_usec) / 1000000.0)
#define TV_DELTA 0.1
/*
* The return codes used by the scli command functions.
*
* Theory of scli return codes:
*
* 1xy - transient messages
* 2xy - positive completion
* 3xy - generic error codes
* 4xy - errors detected before command processing
* 5xy - communication failure
*/
#define SCLI_MSG 100 /* arbitrary message */
#define SCLI_OK 200 /* normal return code */
#define SCLI_EXIT 201 /* return and exit the command loop */
#define SCLI_ERROR 300 /* generic error return code */
#define SCLI_ERROR_NOPEER 301 /* no association to SNMP peer */
#define SCLI_ERROR_NOXML 302 /* command does not support XML */
#define SCLI_SYNTAX 400 /* generic syntax error */
#define SCLI_SYNTAX_NUMARGS 401 /* syntax error in number of args */
#define SCLI_SYNTAX_REGEXP 402 /* syntax error in regexp */
#define SCLI_SYNTAX_NUMBER 403 /* syntax error in number */
#define SCLI_SYNTAX_VALUE 404 /* syntax error in value */
#define SCLI_SYNTAX_TOKENIZER 405 /* syntax error in tokenizer */
#define SCLI_SYNTAX_COMMAND 406 /* syntax error unknown command */
#define SCLI_SNMP 500 /* snmp error return code */
#define SCLI_SNMP_NAME 501 /* snmp name lookup error */
typedef struct scli_interp scli_interp_t;
typedef struct scli_mode scli_mode_t;
typedef struct scli_cmd scli_cmd_t;
typedef struct scli_alias scli_alias_t;
typedef struct scli_alarm scli_alarm_t;
#define SCLI_CMD_FLAG_NEED_PEER (1 << 0)
#define SCLI_CMD_FLAG_MONITOR (1 << 1)
#define SCLI_CMD_FLAG_LOOP (1 << 2)
#define SCLI_CMD_FLAG_XML (1 << 3)
#define SCLI_CMD_FLAG_DRY (1 << 4)
#define SCLI_CMD_FLAG_NORECURSE (1 << 5)
#define SCLI_CMD_FLAG_DYNAMIC (1 << 6)
struct scli_cmd {
char *path; /* absolute command name */
char *options; /* options accepted by the command */
char *desc; /* description of the command */
int flags; /* command flags (see above) */
char *xpath; /* xml path */
char *xsd; /* xml schema definition */
int (*func) (scli_interp_t *interp, int argc, char **argv);
/* function implementing the command */
char *name; /* name of the command (last elem of path) */
};
struct scli_mode {
char *name; /* name of the mode */
char *desc; /* description of the mode */
scli_cmd_t *cmds; /* array of command provided by the mode */
GModule *module; /* module handle (if any) */
};
struct scli_alias {
char *name; /* name of the command alias */
char *value; /* value of the command alias */
};
/*
* The following severity values are aligned with ITU M.3100
* and X.733 and the IETF DISMAN Alarm MIB.
*/
#define SCLI_ALARM_SEVERITY_CLEARED 0x01
#define SCLI_ALARM_SEVERITY_INDETERMINATE 0x02
#define SCLI_ALARM_SEVERITY_CRITICAL 0x03
#define SCLI_ALARM_SEVERITY_MAJOR 0x04
#define SCLI_ALARM_SEVERITY_MINOR 0x05
#define SCLI_ALARM_SEVERITY_WARNING 0x06
/*
* The following trend indication values are aligned with
* ITU M.3100 and X.733 and the IETF DISMAN Alarm MIB.
*/
#define SCLI_ALARM_TREN_MORESEVERE 0x01
#define SCLI_ALARM_TREN_NOCHANGE 0x02
#define SCLI_ALARM_TREN_LESSSEVERE 0x03
struct scli_alarm {
time_t detected; /* detection time of the alarm */
time_t verified; /* last verification of the alarm */
time_t cleared; /* clear time of the alarm */
gchar *desc; /* description of the alarm */
int state; /* state of the alarm */
int severity; /* perceived severity of the alarm */
int trend; /* trend indication */
gpointer detector; /* identification of the detector */
gpointer resource; /* identification of the resource */
};
#define SCLI_INTERP_FLAG_INTERACTIVE 0x001
#define SCLI_INTERP_FLAG_RECURSIVE 0x002
#define SCLI_INTERP_FLAG_MONITOR 0x004
#define SCLI_INTERP_FLAG_LOOP 0x008
#define SCLI_INTERP_FLAG_XML 0x010
#define SCLI_INTERP_FLAG_FMT 0x020
#define SCLI_INTERP_FLAG_DRY 0x040
#define SCLI_INTERP_FLAG_PROTO 0x080
#define SCLI_INTERP_FLAG_QUIET 0x100
#define SCLI_INTERP_FLAG_NOLOOKUP 0x200
struct scli_interp {
char *name; /* name of the interpreter */
GNode *cmd_root; /* root of the command tree */
GSList *mode_list; /* list of registered modes */
GSList *alias_list; /* list of command aliases */
GSList *slave_list; /* list of slave interpreters */
struct scli_interp *master; /* master interpreter (if any) */
GSList *alarm_list; /* list of detected alarms */
int flags; /* interpreter flags */
GString *result; /* string result buffer */
GString *header; /* header line to display */
xmlDocPtr xml_doc; /* xml document if we produce xml output */
xmlNodePtr xml_node; /* current xml node in the xml document */
char *pager; /* external pager we are using */
GNetSnmp *peer; /* snmp session we are using */
gint delay; /* delay between updates in milliseconds */
GNetSnmpTDomain tdomain; /* transport domain */
GInetAddr *taddress; /* transport address */
int snmp; /* version of the preferred SNMP protocol */
time_t epoch; /* epoch (used to invalidate cached data) */
int xid; /* unique identifier for each transaction */
time_t xtime; /* transaction time */
int regex_flags; /* regular expression flags (see regcomp(3)) */
guint retries; /* number of retries */
guint timeout; /* timeout causing a retry in ms */
};
extern scli_interp_t *
scli_interp_create(char *name);
extern void
scli_interp_delete(scli_interp_t *interp);
extern void
scli_interp_init(scli_interp_t *interp);
extern void
scli_interp_reset(scli_interp_t *interp);
#define scli_interp_interactive(interp) \
(interp->flags & SCLI_INTERP_FLAG_INTERACTIVE)
#define scli_interp_recursive(interp) \
(interp->flags & SCLI_INTERP_FLAG_RECURSIVE)
#define scli_interp_xml(interp) \
(interp->flags & SCLI_INTERP_FLAG_XML)
#define scli_interp_monitor(interp) \
(interp->flags & SCLI_INTERP_FLAG_MONITOR)
#define scli_interp_dry(interp) \
(interp->flags & SCLI_INTERP_FLAG_DRY)
#define scli_interp_proto(interp) \
(interp->flags & SCLI_INTERP_FLAG_PROTO)
#define scli_interp_quiet(interp) \
(interp->flags & SCLI_INTERP_FLAG_QUIET)
#define scli_interp_nolookup(interp) \
(interp->flags & SCLI_INTERP_FLAG_NOLOOKUP)
extern scli_alarm_t *
scli_alarm_create(scli_interp_t *interp, char *desc);
extern void
scli_alarm_delete(scli_interp_t *interp, scli_alarm_t *alarm);
extern int
scli_split(char *string, int *argc, char ***argv);
extern int
scli_monitor(scli_interp_t *interp, scli_cmd_t *cmd, int argc, char **argv);
extern int
scli_loop(scli_interp_t *interp, scli_cmd_t *cmd, int argc, char **argv);
extern int
scli_eval_cmd(scli_interp_t *interp, scli_cmd_t *cmd, int argc, char **argv);
extern int
scli_eval_argc_argv(scli_interp_t *interp, int argc, char **argv);
extern int
scli_eval_string(scli_interp_t *interp, char *s);
extern int
scli_eval_file(scli_interp_t *interp, char *path);
extern int
scli_eval_file_stream(scli_interp_t *interp, FILE *stream);
extern int
scli_eval_init_file(scli_interp_t *interp);
extern void
scli_show_results(scli_interp_t *interp, scli_cmd_t *cmd, int code);
extern void
scli_register_mode(scli_interp_t *interp, scli_mode_t *mode);
extern int
scli_create_command(scli_interp_t *interp, scli_cmd_t *cmd);
extern int
scli_open_community(scli_interp_t *interp, GNetSnmpTDomain tdomain,
GInetAddr *addr, char *community);
extern void
scli_close(scli_interp_t *interp);
extern char*
scli_prompt(scli_interp_t *interp);
extern void
scli_get_screen(int *rows, int *cols);
extern void
scli_curses_on(void);
extern void
scli_curses_off(void);
extern int
scli_set_pager(scli_interp_t *interp, const char *pager);
extern int
scli_set_retries(scli_interp_t *interp, const guint retries);
extern int
scli_set_timeout(scli_interp_t *interp, guint timeout);
extern void
scli_snmp_error(scli_interp_t *interp);
/*
* Initialization functions for the various scli modes.
*/
extern void
scli_init_scli_mode(scli_interp_t *interp);
extern void
scli_init_system_mode(scli_interp_t *interp);
extern void
scli_init_entity_mode(scli_interp_t *interp);
extern void
scli_init_interface_mode(scli_interp_t *interp);
extern void
scli_init_disman_mode(scli_interp_t *interp);
extern void
scli_init_snmp_mode(scli_interp_t *interp);
extern void
scli_init_ip_mode(scli_interp_t *interp);
extern void
scli_init_udp_mode(scli_interp_t *interp);
extern void
scli_init_tcp_mode(scli_interp_t *interp);
extern void
scli_init_bridge_mode(scli_interp_t *interp);
extern void
scli_init_atm_mode(scli_interp_t *interp);
extern void
scli_init_ether_mode(scli_interp_t *interp);
extern void
scli_init_ospf_mode(scli_interp_t *interp);
extern void
scli_init_printer_mode(scli_interp_t *interp);
extern void
scli_init_isdn_mode(scli_interp_t *interp);
extern void
scli_init_rs232_mode(scli_interp_t *interp);
extern void
scli_init_sonet_mode(scli_interp_t *interp);
extern void
scli_init_cisco_mode(scli_interp_t *interp);
extern void
scli_init_nortel_mode(scli_interp_t *interp);
extern void
scli_init_netsnmp_mode(scli_interp_t *interp);
extern void
scli_init_3com_mode(scli_interp_t *interp);
extern void
scli_init_hp_mode(scli_interp_t *interp);
/*
* A data structure used to obtain vendor information from the
* enterprise OID. See the file vendors.c for details.
*/
typedef struct scli_vendor {
guint32 id;
char *name;
char *url;
} scli_vendor_t;
extern const scli_vendor_t*
scli_get_vendor(guint32 enterp);
extern const scli_vendor_t*
scli_get_vendor_oid(guint32 oid[], guint16 len);
extern const scli_vendor_t*
scli_get_ieee_vendor(guint32 prefix);
/*
* Formatting and scanning utilities that are used frequently by scli
* modes.
*/
extern const char *
xml_timeticks(guint32 timeticks);
extern const char *
fmt_timeticks(guint32 timeticks);
extern char const *
xml_date_and_time(guchar *data, gsize len);
extern char const *
fmt_date_and_time(guchar *data, gsize len);
extern char const *
fmt_date_and_time_delta(guchar *date1, gsize len1, guchar *date2, gsize len2);
extern char const *
fmt_seconds(guint32 seconds);
extern char const *
fmt_enum(GNetSnmpEnum const *table, gint32 *number);
extern void
xxx_enum(GString *s, int width, GNetSnmpEnum const *table, gint32 *number);
extern char const *
fmt_identity(GNetSnmpIdentity const *table,
guint32 const *oid, gsize oidlen);
extern char const *
fmt_kmg(guint32 number);
extern char const *
fmt_gtp(guint32 number);
#define SCLI_FMT_ADDR 0x01
#define SCLI_FMT_NAME 0x02
#define SCLI_FMT_NAME_OR_ADDR (SCLI_FMT_ADDR | SCLI_FMT_NAME)
extern char*
fmt_udp_port(unsigned short int port, int flags);
extern char*
fmt_tcp_port(unsigned short int port, int flags);
extern char*
fmt_ipv4_address(guchar *addr, int flags);
extern char*
fmt_ipv4_mask(guchar *addr);
extern char*
fmt_ether_address(guchar *addr, int flags);
extern void
fmt_counter_dt(GString *s, guint32 *new_counter, guint32 *old_counter,
struct timeval *last, double delta);
extern void
fmt_indent_string(GString *s, int indent, char *label, int len, char *string);
extern const char*
xml_display_string(int len, char *string);
extern void
fmt_display_string(GString *s, int indent, char *label, int len, char *string);
extern const char*
fmt_tdomain(guint32 *tdomain, gsize tdomain_len);
extern const char*
fmt_taddress(guint32 *tdomain, gsize tdomain_len,
guchar *taddress, gsize taddress_len);
extern void
fmt_row_status(GString *s, gint32 *status);
extern void
fmt_storage_type(GString *s, gint32 *storage);
extern void
fmt_port_set(GString *s, guchar *bits, gsize bits_len);
extern int
scan_port_set(guchar *bits, gsize bits_len, char *string);
/*
* XML utilities that are quite handy and used frequently by scli
* modes.
*/
extern void
xml_set_prop(xmlNodePtr node, const xmlChar *name, const char *fmt, ...)
G_GNUC_PRINTF(3, 4);
extern void
xml_set_content(xmlNodePtr node, const char *fmt, ...)
G_GNUC_PRINTF(2, 3);
extern xmlNodePtr
xml_new_child(xmlNodePtr parent, xmlNsPtr ns, const xmlChar *name,
const char *format, ...) G_GNUC_PRINTF(4, 5);
/*
* Mode specific utilities that are generally useful.
*/
#if 0
extern int
interface_match(regex_t *regex, if_mib_ifEntry_t *ifEntry);
#endif
#endif /* _SCLI_H */
|