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 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
/*
* entity.c -- scli entity mode implementation
*
* 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: entity.c 1758 2006-01-18 09:25:44Z schoenw $
*/
#include "scli.h"
#include "snmpv2-tc.h"
#include "entity-mib.h"
#include "entity-sensor-mib.h"
static void
fmt_containment(GString *s, char *prefix,
entity_mib_entPhysicalEntry_t **entPhysicalEntry,
gint32 parent, int class_width)
{
int i, j;
size_t len;
const char *class;
if (! entPhysicalEntry) {
return;
}
len = strlen(prefix);
for (i = 0; entPhysicalEntry[i]; i++) {
if (entPhysicalEntry[i]->entPhysicalContainedIn
&& *(entPhysicalEntry[i]->entPhysicalContainedIn) == parent) {
int next = 0;
if (parent) {
for (j = i+1; entPhysicalEntry[j]; j++) {
if (entPhysicalEntry[j]->entPhysicalContainedIn
&& entPhysicalEntry[i]->entPhysicalIndex
&& *(entPhysicalEntry[j]->entPhysicalContainedIn)
== parent) {
next = 1;
break;
}
}
}
g_string_sprintfa(s, "%10d",
entPhysicalEntry[i]->entPhysicalIndex);
class = fmt_enum(entity_mib_enums_PhysicalClass,
entPhysicalEntry[i]->entPhysicalClass);
g_string_sprintfa(s, " %-*s", class_width, class ? class : "");
if (len) {
g_string_sprintfa(s, "%s %c- ", prefix, next ? '|' : '`');
} else {
g_string_append(s, " ");
}
g_string_sprintfa(s, "%.*s\n",
(int) entPhysicalEntry[i]->_entPhysicalDescrLength,
entPhysicalEntry[i]->entPhysicalDescr);
if (entPhysicalEntry[i]->entPhysicalIndex) {
char *new_prefix = NULL;
new_prefix = g_malloc(len + 3);
strcpy(new_prefix, prefix);
if (next) {
strcat(new_prefix, len ? " |" : "`");
} else {
strcat(new_prefix, len ? " " : " ");
}
fmt_containment(s, new_prefix, entPhysicalEntry,
entPhysicalEntry[i]->entPhysicalIndex,
class_width);
g_free(new_prefix);
}
}
}
}
static int
show_entity_containment(scli_interp_t *interp, int argc, char **argv)
{
entity_mib_entPhysicalEntry_t **entPhysicalTable = NULL;
int class_width = 6;
int i;
const char *class;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
entity_mib_get_entPhysicalTable(interp->peer, &entPhysicalTable, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (entPhysicalTable) {
for (i = 0; entPhysicalTable[i]; i++) {
class = fmt_enum(entity_mib_enums_PhysicalClass,
entPhysicalTable[i]->entPhysicalClass);
if (strlen(class) > class_width) {
class_width = strlen(class);
}
}
g_string_sprintfa(interp->header, " ENTITY %-*s CONTAINMENT",
class_width, "CLASS");
fmt_containment(interp->result, "", entPhysicalTable, 0, class_width);
}
if (entPhysicalTable) entity_mib_free_entPhysicalTable(entPhysicalTable);
return SCLI_OK;
}
static void
fmt_entity_sensors(GString *s,
entity_sensor_mib_entPhySensorEntry_t *entSensorEntry)
{
g_string_sprintfa(s, "sensor %d\n",
entSensorEntry->entPhysicalIndex);
}
static int
show_entity_sensors(scli_interp_t *interp, int argc, char **argv)
{
entity_sensor_mib_entPhySensorEntry_t **entSensorTable = 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;
}
entity_sensor_mib_get_entPhySensorTable(interp->peer, &entSensorTable, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (entSensorTable) {
for (i = 0; entSensorTable[i]; i++) {
fmt_entity_sensors(interp->result, entSensorTable[i]);
}
}
if (entSensorTable) entity_sensor_mib_free_entPhySensorTable(entSensorTable);
return SCLI_OK;
}
static void
fmt_entity_details(GString *s, entity_mib_entPhysicalEntry_t *entPhysicalEntry)
{
const char *class;
g_string_sprintfa(s, "Entity: %-*d", 30,
entPhysicalEntry->entPhysicalIndex);
if (entPhysicalEntry->entPhysicalName) {
g_string_sprintfa(s, "Name: %.*s\n",
(int) entPhysicalEntry->_entPhysicalNameLength,
entPhysicalEntry->entPhysicalName);
} else {
g_string_sprintfa(s, "Name:\n");
}
if (entPhysicalEntry->entPhysicalMfgName) {
g_string_sprintfa(s, "Vendor: %-30.*s",
(int) entPhysicalEntry->_entPhysicalMfgNameLength,
entPhysicalEntry->entPhysicalMfgName);
} else {
g_string_sprintfa(s, "Vendor: %-30s", "");
}
if (entPhysicalEntry->entPhysicalHardwareRev) {
g_string_sprintfa(s, "HW Rev: %-30.*s\n",
(int) entPhysicalEntry->_entPhysicalHardwareRevLength,
entPhysicalEntry->entPhysicalHardwareRev);
} else {
g_string_sprintfa(s, "HW Rev:\n");
}
if (entPhysicalEntry->entPhysicalModelName) {
g_string_sprintfa(s, "Model: %-30.*s",
(int) entPhysicalEntry->_entPhysicalModelNameLength,
entPhysicalEntry->entPhysicalModelName);
} else {
g_string_sprintfa(s, "Model: %-30s", "");
}
if (entPhysicalEntry->entPhysicalFirmwareRev) {
g_string_sprintfa(s, "FW Rev: %-30.*s\n",
(int) entPhysicalEntry->_entPhysicalFirmwareRevLength,
entPhysicalEntry->entPhysicalFirmwareRev);
} else {
g_string_sprintfa(s, "FW Rev:\n");
}
if (entPhysicalEntry->entPhysicalSerialNum) {
g_string_sprintfa(s, "Serial: %-30.*s",
(int) entPhysicalEntry->_entPhysicalSerialNumLength,
entPhysicalEntry->entPhysicalSerialNum);
} else {
g_string_sprintfa(s, "Serial: %-30s", "");
}
if (entPhysicalEntry->entPhysicalSoftwareRev) {
g_string_sprintfa(s, "SW Rev: %-30.*s\n",
(int) entPhysicalEntry->_entPhysicalSoftwareRevLength,
entPhysicalEntry->entPhysicalSoftwareRev);
} else {
g_string_sprintfa(s, "SW Rev:\n");
}
if (entPhysicalEntry->entPhysicalAssetID) {
g_string_sprintfa(s, "Asset: %-30.*s",
(int) entPhysicalEntry->_entPhysicalAssetIDLength,
entPhysicalEntry->entPhysicalAssetID);
} else {
g_string_sprintfa(s, "Asset: %-30s", "");
}
class = fmt_enum(entity_mib_enums_PhysicalClass,
entPhysicalEntry->entPhysicalClass);
if (class) {
g_string_sprintfa(s, "Class: %s", class);
if (entPhysicalEntry->entPhysicalIsFRU) {
if (*entPhysicalEntry->entPhysicalIsFRU
== SNMPV2_TC_TRUTHVALUE_TRUE) {
g_string_append(s, " (replaceable)");
}
}
g_string_append(s, "\n");
}
fmt_display_string(s, 8, "Alias:",
(int) entPhysicalEntry->_entPhysicalAliasLength,
entPhysicalEntry->entPhysicalAlias);
fmt_display_string(s, 8, "Descr:",
(int) entPhysicalEntry->_entPhysicalDescrLength,
entPhysicalEntry->entPhysicalDescr);
}
static void
xml_entity_details(xmlNodePtr root,
entity_mib_entPhysicalEntry_t *entPhysicalEntry)
{
xmlNodePtr tree;
const char *e;
tree = xmlNewChild(root, NULL, "entity", NULL);
xml_set_prop(tree, "index", "%d", entPhysicalEntry->entPhysicalIndex);
if (entPhysicalEntry->entPhysicalDescr) {
(void) xml_new_child(tree, NULL, "description", "%.*s",
(int) entPhysicalEntry->_entPhysicalDescrLength,
entPhysicalEntry->entPhysicalDescr);
}
if (entPhysicalEntry->entPhysicalName) {
(void) xml_new_child(tree, NULL, "name", "%.*s",
(int) entPhysicalEntry->_entPhysicalNameLength,
entPhysicalEntry->entPhysicalName);
}
e = fmt_enum(entity_mib_enums_PhysicalClass,
entPhysicalEntry->entPhysicalClass);
if (e) {
(void) xml_new_child(tree, NULL, "class", "%s", e);
}
if (entPhysicalEntry->entPhysicalContainedIn) {
(void) xml_new_child(tree, NULL, "contained-in", "%d",
*entPhysicalEntry->entPhysicalContainedIn);
}
if (entPhysicalEntry->entPhysicalIsFRU
&& (*entPhysicalEntry->entPhysicalIsFRU
== SNMPV2_TC_TRUTHVALUE_TRUE)) {
(void) xmlNewChild(tree, NULL, "replaceable", NULL);
}
if (entPhysicalEntry->entPhysicalMfgName) {
(void) xml_new_child(tree, NULL, "manufacturer", "%.*s",
(int) entPhysicalEntry->_entPhysicalMfgNameLength,
entPhysicalEntry->entPhysicalMfgName);
}
if (entPhysicalEntry->entPhysicalModelName) {
(void) xml_new_child(tree, NULL, "model", "%.*s",
(int) entPhysicalEntry->_entPhysicalModelNameLength,
entPhysicalEntry->entPhysicalModelName);
}
if (entPhysicalEntry->entPhysicalSerialNum) {
(void) xml_new_child(tree, NULL, "serial", "%.*s",
(int) entPhysicalEntry->_entPhysicalSerialNumLength,
entPhysicalEntry->entPhysicalSerialNum);
}
if (entPhysicalEntry->entPhysicalHardwareRev) {
(void) xml_new_child(tree, NULL, "hardware-revision", "%.*s",
(int) entPhysicalEntry->_entPhysicalHardwareRevLength,
entPhysicalEntry->entPhysicalHardwareRev);
}
if (entPhysicalEntry->entPhysicalFirmwareRev) {
(void) xml_new_child(tree, NULL, "firmware-revision", "%.*s",
(int) entPhysicalEntry->_entPhysicalFirmwareRevLength,
entPhysicalEntry->entPhysicalFirmwareRev);
}
if (entPhysicalEntry->entPhysicalSoftwareRev) {
(void) xml_new_child(tree, NULL, "software-revision", "%.*s",
(int) entPhysicalEntry->_entPhysicalSoftwareRevLength,
entPhysicalEntry->entPhysicalSoftwareRev);
}
if (entPhysicalEntry->entPhysicalAlias) {
(void) xml_new_child(tree, NULL, "alias", "%.*s",
(int) entPhysicalEntry->_entPhysicalAliasLength,
entPhysicalEntry->entPhysicalAlias);
}
if (entPhysicalEntry->entPhysicalAssetID) {
(void) xml_new_child(tree, NULL, "asset","%.*s",
(int) entPhysicalEntry->_entPhysicalAssetIDLength,
entPhysicalEntry->entPhysicalAssetID);
}
}
static int
show_entity_details(scli_interp_t *interp, int argc, char **argv)
{
entity_mib_entPhysicalEntry_t **entPhysicalTable = 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;
}
entity_mib_get_entPhysicalTable(interp->peer, &entPhysicalTable, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (entPhysicalTable) {
for (i = 0; entPhysicalTable[i]; i++) {
if (scli_interp_xml(interp)) {
xml_entity_details(interp->xml_node, entPhysicalTable[i]);
} else {
if (i) {
g_string_append(interp->result, "\n");
}
fmt_entity_details(interp->result, entPhysicalTable[i]);
}
}
}
if (entPhysicalTable) entity_mib_free_entPhysicalTable(entPhysicalTable);
return SCLI_OK;
}
static void
fmt_entity_info(GString *s, entity_mib_entPhysicalEntry_t *entPhysicalEntry,
int class_width, int name_width)
{
const char *class;
g_string_sprintfa(s, "%10d", entPhysicalEntry->entPhysicalIndex);
class = fmt_enum(entity_mib_enums_PhysicalClass,
entPhysicalEntry->entPhysicalClass);
if (class) {
g_string_sprintfa(s, " %-*s", class_width, class);
} else {
g_string_sprintfa(s, " %-*s", class_width, "");
}
if (entPhysicalEntry->entPhysicalName) {
g_string_sprintfa(s, " %-*.*s", name_width,
(int) entPhysicalEntry->_entPhysicalNameLength,
entPhysicalEntry->entPhysicalName);
} else {
g_string_sprintfa(s, " %-*s", name_width, "");
}
if (entPhysicalEntry->entPhysicalDescr) {
g_string_sprintfa(s, " %.*s",
(int) entPhysicalEntry->_entPhysicalDescrLength,
entPhysicalEntry->entPhysicalDescr);
}
g_string_append(s, "\n");
}
static int
show_entity_info(scli_interp_t *interp, int argc, char **argv)
{
entity_mib_entPhysicalEntry_t **entPhysicalTable = NULL;
int class_width = 6, name_width = 6;
int i;
const char *class;
g_return_val_if_fail(interp, SCLI_ERROR);
if (argc > 1) {
return SCLI_SYNTAX_NUMARGS;
}
if (scli_interp_dry(interp)) {
return SCLI_OK;
}
entity_mib_get_entPhysicalTable(interp->peer, &entPhysicalTable, 0);
if (interp->peer->error_status) {
return SCLI_SNMP;
}
if (entPhysicalTable) {
for (i = 0; entPhysicalTable[i]; i++) {
class = fmt_enum(entity_mib_enums_PhysicalClass,
entPhysicalTable[i]->entPhysicalClass);
if (strlen(class) > class_width) {
class_width = strlen(class);
}
if (entPhysicalTable[i]->entPhysicalName
&& entPhysicalTable[i]->_entPhysicalNameLength > name_width) {
name_width = entPhysicalTable[i]->_entPhysicalNameLength;
}
}
g_string_sprintfa(interp->header,
" ENTITY %-*s %-*s DESCRIPTION",
class_width, "CLASS",
name_width, "NAME");
for (i = 0; entPhysicalTable[i]; i++) {
fmt_entity_info(interp->result, entPhysicalTable[i],
class_width, name_width);
}
}
if (entPhysicalTable) entity_mib_free_entPhysicalTable(entPhysicalTable);
return SCLI_OK;
}
void
scli_init_entity_mode(scli_interp_t *interp)
{
static scli_cmd_t cmds[] = {
{ "show entity info", NULL,
"The `show entity info' command displays summary information about\n"
"the physical entities that compose the system. The command\n"
"generates a table with the following columns:\n"
"\n"
" ENTITY entity number\n"
" CLASS class of the entity (see below)\n"
" NAME name of the entity\n"
" DESCRIPTION description of the entity",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
NULL, NULL,
show_entity_info },
{ "show entity details ", NULL,
"The `show entity details' command describes the physical entities\n"
"in more detail.",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_DRY,
"entities physical", NULL,
show_entity_details },
{ "show entity containment", NULL,
"The `show entity containment' command displays the physical entity\n"
"containment hierarchy.",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_DRY,
NULL, NULL,
show_entity_containment },
{ "show entity sensors ", NULL,
"The `show entity sensors' command describes the physical sensor\n"
"entities in more detail.",
SCLI_CMD_FLAG_NEED_PEER | SCLI_CMD_FLAG_XML | SCLI_CMD_FLAG_DRY,
"entities sensors", NULL,
show_entity_sensors },
#if 0
{ "set entity serial", NULL,
"xxx",
SCLI_CMD_FLAG_NEED_PEER,
NULL, NULL,
set_entity_serial },
{ "set entity alias", NULL,
"xxx",
SCLI_CMD_FLAG_NEED_PEER,
NULL, NULL,
set_entity_alias },
{ "set entity asset", NULL,
"xxx",
SCLI_CMD_FLAG_NEED_PEER,
NULL, NULL,
set_entity_asset },
{ "dump entity", NULL,
"xxx",
SCLI_CMD_FLAG_NEED_PEER,
NULL, NULL,
set_entity_serial },
#endif
#if 0
/* ENTITY NAME STATUS UNIT VALUE UPDATE-RATE LAST-UPDATE */
#endif
{ NULL, NULL, NULL, 0, NULL, NULL, NULL }
};
static scli_mode_t entity_mode = {
"entity",
"The entity scli mode is based on the ENTITY-MIB as published in\n"
"RFC 2737. It provides commands to browse the physical entities\n"
"or physical components that make up a managed system.",
cmds
};
scli_register_mode(interp, &entity_mode);
}
|