xref: /titanic_50/usr/src/cmd/fm/ipmitopo/common/ipmitopo.c (revision d6114e2d100d9ec3b45f9968d45ac2e3a0827af0)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <libipmi.h>
27 #include <stdio.h>
28 #include <string.h>
29 
30 /*ARGSUSED*/
31 static int
32 sdr_print(ipmi_handle_t *ihp, ipmi_entity_t *ep, const char *name,
33     ipmi_sdr_t *sdrp, void *data)
34 {
35 	int indentation = (uintptr_t)data;
36 	ipmi_sdr_compact_sensor_t *csp;
37 	ipmi_sdr_full_sensor_t *fsp;
38 	uint8_t sensor_number, sensor_type, reading_type;
39 	boolean_t get_reading = B_FALSE;
40 	ipmi_sensor_reading_t *srp;
41 	char sensor_name[128];
42 	char reading_name[128];
43 
44 	if (name == NULL)
45 		return (0);
46 
47 	switch (sdrp->is_type) {
48 	case IPMI_SDR_TYPE_COMPACT_SENSOR:
49 		csp = (ipmi_sdr_compact_sensor_t *)sdrp->is_record;
50 		sensor_number = csp->is_cs_number;
51 		sensor_type = csp->is_cs_type;
52 		reading_type = csp->is_cs_reading_type;
53 		get_reading = B_TRUE;
54 		break;
55 
56 	case IPMI_SDR_TYPE_FULL_SENSOR:
57 		fsp = (ipmi_sdr_full_sensor_t *)sdrp->is_record;
58 		sensor_number = fsp->is_fs_number;
59 		sensor_type = fsp->is_fs_type;
60 		reading_type = fsp->is_fs_reading_type;
61 		get_reading = B_TRUE;
62 		break;
63 	}
64 
65 	(void) printf("%*s%-*s", indentation, "",
66 	    36 - indentation, name);
67 
68 	if (get_reading) {
69 		ipmi_sensor_type_name(sensor_type, sensor_name,
70 		    sizeof (sensor_name));
71 		ipmi_sensor_reading_name(sensor_type, reading_type,
72 		    reading_name, sizeof (reading_name));
73 		(void) printf("%12s  %12s", sensor_name, reading_name);
74 		if ((srp = ipmi_get_sensor_reading(ihp,
75 		    sensor_number)) == NULL) {
76 			if (ipmi_errno(ihp) == EIPMI_NOT_PRESENT) {
77 				(void) printf("      -\n");
78 			} else {
79 				(void) printf("\n");
80 				return (-1);
81 			}
82 		} else {
83 			(void) printf("   %04x\n", srp->isr_state);
84 		}
85 	} else {
86 		(void) printf("\n");
87 	}
88 
89 	return (0);
90 }
91 
92 static int
93 entity_print(ipmi_handle_t *ihp, ipmi_entity_t *ep, void *data)
94 {
95 	int indentation = (uintptr_t)data;
96 	char name[128];
97 	boolean_t present;
98 
99 	ipmi_entity_name(ep->ie_type, name, sizeof (name));
100 	(void) snprintf(name + strlen(name), sizeof (name) - strlen(name),
101 	    " %d", ep->ie_instance);
102 
103 	if (ipmi_entity_present(ihp, ep, &present) != 0) {
104 		(void) printf("%*s%-*s  %s (%s)\n", indentation, "",
105 		    24 - indentation, name, "unknown", ipmi_errmsg(ihp));
106 	} else {
107 		(void) printf("%*s%-*s  %s\n", indentation, "",
108 		    24 - indentation, name, present ? "present" : "absent");
109 	}
110 	(void) ipmi_entity_iter_sdr(ihp, ep, sdr_print,
111 	    (void *)(indentation + 2));
112 
113 	if (ep->ie_children != 0)
114 		(void) ipmi_entity_iter_children(ihp, ep, entity_print,
115 		    (void *)(indentation + 2));
116 	return (0);
117 }
118 
119 /*ARGSUSED*/
120 int
121 main(int argc, char **argv)
122 {
123 	ipmi_handle_t *ihp;
124 	char *errmsg;
125 	int err;
126 
127 	if ((ihp = ipmi_open(&err, &errmsg)) == NULL) {
128 		(void) fprintf(stderr, "failed to open libipmi: %s\n",
129 		    errmsg);
130 		return (1);
131 	}
132 
133 	(void) printf("%-24s  %-8s  %12s  %12s  %5s\n",
134 	    "ENTITY/SENSOR", "PRESENT", "SENSOR", "READING", "STATE");
135 	(void) printf("-----------------------  --------  -------------  "
136 	    "------------  -----\n");
137 	if (ipmi_entity_iter(ihp, entity_print, NULL) != 0) {
138 		(void) fprintf(stderr, "failed to iterate entities: %s\n",
139 		    ipmi_errmsg(ihp));
140 		return (1);
141 	}
142 
143 	ipmi_close(ihp);
144 
145 	return (0);
146 }
147