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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
* Copyright 2020 Joyent, Inc.
*/
#include <sys/types.h>
#include <fmadm.h>
#include <errno.h>
#include <limits.h>
#include <strings.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fm/fmd_log.h>
#include <sys/fm/protocol.h>
#include <fm/libtopo.h>
#include <fm/fmd_adm.h>
#include <fm/fmd_msg.h>
#include <dlfcn.h>
#include <sys/systeminfo.h>
#include <sys/utsname.h>
#include <libintl.h>
#include <locale.h>
#include <sys/smbios.h>
#include <libdevinfo.h>
#include <stdlib.h>
#include <stddef.h>
/*
* Fault records are added to catalog by calling add_fault_record_to_catalog()
* records are stored in order of importance to the system.
* If -g flag is set or not_suppressed is not set and the class fru, fault,
* type are the same then details are merged into an existing record, with uuid
* records are stored in time order.
* For each record information is extracted from nvlist and merged into linked
* list each is checked for identical records for which percentage certainty are
* added together.
* print_catalog() is called to print out catalog and release external resources
*
* /---------------\
* status_rec_list -> | | -|
* \---------------/
* \/
* /---------------\ /-------\ /-------\
* status_fru_list | status_record | -> | uurec | -> | uurec | -|
* \/ | | |- | | <- | |
* /-------------\ | | \-------/ \-------/
* | | -> | | \/ \/
* \-------------/ | | /-------\ /-------\
* \/ | | -> | asru | -> | asru |
* --- | | | | <- | |
* | | \-------/ \-------/
* status_asru_list | class |
* \/ | resource | /-------\ /-------\
* /-------------\ | fru | -> | list | -> | list |
* | | -> | serial | | | <- | |
* \-------------/ | | \-------/ \-------/
* \/ \---------------/
* --- \/ /\
* /---------------\
* | status_record |
* \---------------/
*
* Fmadm faulty takes a number of options which affect the format of the
* output displayed. By default, the display reports the FRU and ASRU along
* with other information on per-case basis as in the example below.
*
* --------------- ------------------------------------ -------------- -------
* TIME EVENT-ID MSG-ID SEVERITY
* --------------- ------------------------------------ -------------- -------
* Sep 21 10:01:36 d482f935-5c8f-e9ab-9f25-d0aaafec1e6c AMD-8000-2F Major
*
* Fault class : fault.memory.dimm_sb
* Affects : mem:///motherboard=0/chip=0/memory-controller=0/dimm=0/rank=0
* faulted but still in service
* FRU : "CPU 0 DIMM 0" (hc://.../memory-controller=0/dimm=0)
* faulty
*
* Description : The number of errors associated with this memory module has
* exceeded acceptable levels. Refer to
* http://illumos.org/msg/AMD-8000-2F for more information.
*
* Response : Pages of memory associated with this memory module are being
* removed from service as errors are reported.
*
* Impact : Total system memory capacity will be reduced as pages are
* retired.
*
* Action : Schedule a repair procedure to replace the affected memory
* module. Use fmdump -v -u <EVENT_ID> to identify the module.
*
* The -v flag is similar, but adds some additonal information such as the
* resource. The -s flag is also similar but just gives the top line summary.
* All these options (ie without the -f or -r flags) use the print_catalog()
* function to do the display.
*
* The -f flag changes the output so that it appears sorted on a per-fru basis.
* The output is somewhat cut down compared to the default output. If -f is
* used, then print_fru() is used to print the output.
*
* -----------------------------------------------------------------------------
* "SLOT 2" (hc://.../hostbridge=3/pciexrc=3/pciexbus=4/pciexdev=0) faulty
* 5ca4aeb3-36...f6be-c2e8166dc484 2 suspects in this FRU total certainty 100%
*
* Description : A problem was detected for a PCI device.
* Refer to http://illumos.org/msg/PCI-8000-7J
* for more information.
*
* Response : One or more device instances may be disabled
*
* Impact : Possible loss of services provided by the device instances
* associated with this fault
*
* Action : Schedule a repair procedure to replace the affected device.
* Use fmdump -v -u <EVENT_ID> to identify the device or contact
* Sun for support.
*
* The -r flag changes the output so that it appears sorted on a per-asru basis.
* The output is very much cut down compared to the default output, just giving
* the asru fmri and state. Here print_asru() is used to print the output.
*
* mem:///motherboard=0/chip=0/memory-controller=0/dimm=0/rank=0 degraded
*
* For all fmadm faulty options, the sequence of events is
*
* 1) Walk through all the cases in the system using fmd_adm_case_iter() and
* for each case call dfault_rec(). This will call add_fault_record_to_catalog()
* This will extract the data from the nvlist and call catalog_new_record() to
* save the data away in various linked lists in the catalogue.
*
* 2) Once this is done, the data can be supplemented by using
* fmd_adm_rsrc_iter(). However this is now only necessary for the -i option.
*
* 3) Finally print_catalog(), print_fru() or print_asru() are called as
* appropriate to display the information from the catalogue sorted in the
* requested way.
*
*/
typedef struct name_list {
struct name_list *next;
struct name_list *prev;
char *name;
uint8_t pct;
uint8_t max_pct;
ushort_t count;
int status;
char *label;
} name_list_t;
typedef struct ari_list {
char *ari_uuid;
struct ari_list *next;
} ari_list_t;
typedef struct uurec {
struct uurec *next;
struct uurec *prev;
char *uuid;
ari_list_t *ari_uuid_list;
name_list_t *asru;
uint64_t sec;
nvlist_t *event;
} uurec_t;
typedef struct uurec_select {
struct uurec_select *next;
char *uuid;
} uurec_select_t;
typedef struct host_id {
char *chassis;
char *server;
char *platform;
char *domain;
char *product_sn;
} hostid_t;
typedef struct host_id_list {
hostid_t hostid;
struct host_id_list *next;
} host_id_list_t;
typedef struct status_record {
hostid_t *host;
int nrecs;
uurec_t *uurec;
char *severity; /* in C locale */
char *msgid;
name_list_t *class;
name_list_t *resource;
name_list_t *asru;
name_list_t *fru;
name_list_t *serial;
uint8_t not_suppressed;
uint8_t injected;
} status_record_t;
typedef struct sr_list {
struct sr_list *next;
struct sr_list *prev;
struct status_record *status_record;
} sr_list_t;
typedef struct resource_list {
struct resource_list *next;
struct resource_list *prev;
sr_list_t *status_rec_list;
char *resource;
uint8_t not_suppressed;
uint8_t injected;
uint8_t max_pct;
} resource_list_t;
sr_list_t *status_rec_list;
resource_list_t *status_fru_list;
resource_list_t *status_asru_list;
static int max_display;
static int max_fault = 0;
static topo_hdl_t *topo_handle;
static host_id_list_t *host_list;
static int n_server;
static int opt_g;
static fmd_msg_hdl_t *fmadm_msghdl = NULL; /* handle for libfmd_msg calls */
static char *
format_date(char *buf, size_t len, uint64_t sec)
{
if (sec > LONG_MAX) {
(void) fprintf(stderr,
"record time is too large for 32-bit utility\n");
(void) snprintf(buf, len, "0x%llx", sec);
} else {
time_t tod = (time_t)sec;
time_t now = time(NULL);
if (tod > now+60 ||
tod < now - 6L*30L*24L*60L*60L) { /* 6 months ago */
(void) strftime(buf, len, "%b %d %Y ",
localtime(&tod));
} else {
(void) strftime(buf, len, "%b %d %T", localtime(&tod));
}
}
return (buf);
}
static hostid_t *
find_hostid_in_list(char *platform, char *chassis, char *server, char *domain,
char *product_sn)
{
hostid_t *rt = NULL;
host_id_list_t *hostp;
if (platform == NULL)
platform = "-";
if (server == NULL)
server = "-";
hostp = host_list;
while (hostp) {
if (hostp->hostid.platform &&
strcmp(hostp->hostid.platform, platform) == 0 &&
hostp->hostid.server &&
strcmp(hostp->hostid.server, server) == 0 &&
(chassis == NULL || hostp->hostid.chassis == NULL ||
strcmp(chassis, hostp->hostid.chassis) == 0) &&
(product_sn == NULL || hostp->hostid.product_sn == NULL ||
strcmp(product_sn, hostp->hostid.product_sn) == 0) &&
(domain == NULL || hostp->hostid.domain == NULL ||
strcmp(domain, hostp->hostid.domain) == 0)) {
rt = &hostp->hostid;
break;
}
hostp = hostp->next;
}
if (rt == NULL) {
hostp = malloc(sizeof (host_id_list_t));
hostp->hostid.platform = strdup(platform);
hostp->hostid.product_sn =
product_sn ? strdup(product_sn) : NULL;
hostp->hostid.server = strdup(server);
hostp->hostid.chassis = chassis ? strdup(chassis) : NULL;
hostp->hostid.domain = domain ? strdup(domain) : NULL;
hostp->next = host_list;
host_list = hostp;
rt = &hostp->hostid;
n_server++;
}
return (rt);
}
static hostid_t *
find_hostid(nvlist_t *nvl)
{
char *platform = NULL, *chassis = NULL, *server = NULL, *domain = NULL;
char *product_sn = NULL;
nvlist_t *auth, *fmri;
hostid_t *rt = NULL;
if (nvlist_lookup_nvlist(nvl, FM_SUSPECT_DE, &fmri) == 0 &&
nvlist_lookup_nvlist(fmri, FM_FMRI_AUTHORITY, &auth) == 0) {
(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT,
&platform);
(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_PRODUCT_SN,
&product_sn);
(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_SERVER, &server);
(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_CHASSIS,
&chassis);
(void) nvlist_lookup_string(auth, FM_FMRI_AUTH_DOMAIN, &domain);
rt = find_hostid_in_list(platform, chassis, server,
domain, product_sn);
}
return (rt);
}
static char *
get_nvl2str_topo(nvlist_t *nvl)
{
char *name = NULL;
char *tname;
int err;
char *scheme = NULL;
char *mod_name = NULL;
char buf[128];
if (topo_handle == NULL)
topo_handle = topo_open(TOPO_VERSION, 0, &err);
if (topo_fmri_nvl2str(topo_handle, nvl, &tname, &err) == 0) {
name = strdup(tname);
topo_hdl_strfree(topo_handle, tname);
} else {
(void) nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &scheme);
(void) nvlist_lookup_string(nvl, FM_FMRI_MOD_NAME, &mod_name);
if (scheme && strcmp(scheme, FM_FMRI_SCHEME_FMD) == 0 &&
mod_name) {
(void) snprintf(buf, sizeof (buf), "%s:///module/%s",
scheme, mod_name);
name = strdup(buf);
}
}
return (name);
}
static int
set_priority(char *s)
{
int rt = 0;
if (s) {
if (strcmp(s, "Minor") == 0)
rt = 1;
else if (strcmp(s, "Major") == 0)
rt = 10;
else if (strcmp(s, "Critical") == 0)
rt = 100;
}
return (rt);
}
static int
cmp_priority(char *s1, char *s2, uint64_t t1, uint64_t t2, uint8_t p1,
uint8_t p2)
{
int r1, r2;
int rt;
r1 = set_priority(s1);
r2 = set_priority(s2);
rt = r1 - r2;
if (rt == 0) {
if (t1 > t2)
rt = 1;
else if (t1 < t2)
rt = -1;
else
rt = p1 - p2;
}
return (rt);
}
/*
* merge two lists into one, by comparing enties in new and moving into list if
* name is not there or free off memory for names which are already there
* add_pct indicates if pct is the sum or highest pct
*/
static name_list_t *
merge_name_list(name_list_t **list, name_list_t *new, int add_pct)
{
name_list_t *lp, *np, *sp, *rt = NULL;
int max_pct;
rt = *list;
np = new;
while (np) {
lp = *list;
while (lp) {
if (strcmp(lp->name, np->name) == 0)
break;
lp = lp->next;
if (lp == *list)
lp = NULL;
}
if (np->next == new)
sp = NULL;
else
sp = np->next;
if (lp) {
lp->status |= (np->status & FM_SUSPECT_FAULTY);
if (add_pct) {
lp->pct += np->pct;
lp->count += np->count;
} else if (np->pct > lp->pct) {
lp->pct = np->pct;
}
max_pct = np->max_pct;
if (np->label)
free(np->label);
free(np->name);
free(np);
np = NULL;
if (max_pct > lp->max_pct) {
lp->max_pct = max_pct;
if (lp->max_pct > lp->prev->max_pct &&
lp != *list) {
lp->prev->next = lp->next;
lp->next->prev = lp->prev;
np = lp;
}
}
}
if (np) {
lp = *list;
if (lp) {
if (np->max_pct > lp->max_pct) {
np->next = lp;
np->prev = lp->prev;
lp->prev->next = np;
lp->prev = np;
*list = np;
rt = np;
} else {
lp = lp->next;
while (lp != *list &&
np->max_pct < lp->max_pct) {
lp = lp->next;
}
np->next = lp;
np->prev = lp->prev;
lp->prev->next = np;
lp->prev = np;
}
} else {
*list = np;
np->next = np;
np->prev = np;
rt = np;
}
}
np = sp;
}
return (rt);
}
static name_list_t *
alloc_name_list(char *name, uint8_t pct)
{
name_list_t *nlp;
nlp = malloc(sizeof (*nlp));
nlp->name = strdup(name);
nlp->pct = pct;
nlp->max_pct = pct;
nlp->count = 1;
nlp->next = nlp;
nlp->prev = nlp;
nlp->status = 0;
nlp->label = NULL;
return (nlp);
}
static status_record_t *
new_record_init(uurec_t *uurec_p, char *msgid, name_list_t *class,
name_list_t *fru, name_list_t *asru, name_list_t *resource,
name_list_t *serial, boolean_t not_suppressed,
hostid_t *hostid, boolean_t injected)
{
status_record_t *status_rec_p;
status_rec_p = (status_record_t *)malloc(sizeof (status_record_t));
status_rec_p->nrecs = 1;
status_rec_p->host = hostid;
status_rec_p->uurec = uurec_p;
uurec_p->next = NULL;
uurec_p->prev = NULL;
uurec_p->asru = asru;
if ((status_rec_p->severity = fmd_msg_getitem_id(fmadm_msghdl, NULL,
msgid, FMD_MSG_ITEM_SEVERITY)) == NULL)
status_rec_p->severity = strdup("unknown");
status_rec_p->class = class;
status_rec_p->fru = fru;
status_rec_p->asru = asru;
status_rec_p->resource = resource;
status_rec_p->serial = serial;
status_rec_p->msgid = strdup(msgid);
status_rec_p->not_suppressed = not_suppressed;
status_rec_p->injected = injected;
return (status_rec_p);
}
/*
* add record to given list maintaining order higher priority first.
*/
static void
add_rec_list(status_record_t *status_rec_p, sr_list_t **list_pp)
{
sr_list_t *tp, *np, *sp;
int order;
uint64_t sec;
np = malloc(sizeof (sr_list_t));
np->status_record = status_rec_p;
sec = status_rec_p->uurec->sec;
if ((sp = *list_pp) == NULL) {
*list_pp = np;
np->next = np;
np->prev = np;
} else {
/* insert new record in front of lower priority */
tp = sp;
order = cmp_priority(status_rec_p->severity,
sp->status_record->severity, sec,
tp->status_record->uurec->sec, 0, 0);
if (order > 0) {
*list_pp = np;
} else {
tp = sp->next;
while (tp != sp &&
cmp_priority(status_rec_p->severity,
tp->status_record->severity, sec,
tp->status_record->uurec->sec, 0, 0)) {
tp = tp->next;
}
}
np->next = tp;
np->prev = tp->prev;
tp->prev->next = np;
tp->prev = np;
}
}
static void
add_resource(status_record_t *status_rec_p, resource_list_t **rp,
resource_list_t *np)
{
int order;
uint64_t sec;
resource_list_t *sp, *tp;
status_record_t *srp;
char *severity = status_rec_p->severity;
add_rec_list(status_rec_p, &np->status_rec_list);
if ((sp = *rp) == NULL) {
np->next = np;
np->prev = np;
*rp = np;
} else {
/*
* insert new record in front of lower priority
*/
tp = sp->next;
srp = sp->status_rec_list->status_record;
sec = status_rec_p->uurec->sec;
order = cmp_priority(severity, srp->severity, sec,
srp->uurec->sec, np->max_pct, sp->max_pct);
if (order > 0) {
*rp = np;
} else {
srp = tp->status_rec_list->status_record;
while (tp != sp &&
cmp_priority(severity, srp->severity, sec,
srp->uurec->sec, np->max_pct, sp->max_pct) < 0) {
tp = tp->next;
srp = tp->status_rec_list->status_record;
}
}
np->next = tp;
np->prev = tp->prev;
tp->prev->next = np;
tp->prev = np;
}
}
static void
add_resource_list(status_record_t *status_rec_p, name_list_t *fp,
resource_list_t **rpp)
{
int order;
resource_list_t *np, *end;
status_record_t *srp;
np = *rpp;
end = np;
while (np) {
if (strcmp(fp->name, np->resource) == 0) {
np->not_suppressed |= status_rec_p->not_suppressed;
np->injected |= status_rec_p->injected;
srp = np->status_rec_list->status_record;
order = cmp_priority(status_rec_p->severity,
srp->severity, status_rec_p->uurec->sec,
srp->uurec->sec, fp->max_pct, np->max_pct);
if (order > 0 && np != end) {
/*
* remove from list and add again using
* new priority
*/
np->prev->next = np->next;
np->next->prev = np->prev;
add_resource(status_rec_p,
rpp, np);
} else {
add_rec_list(status_rec_p,
&np->status_rec_list);
}
break;
}
np = np->next;
if (np == end) {
np = NULL;
break;
}
}
if (np == NULL) {
np = malloc(sizeof (resource_list_t));
np->resource = fp->name;
np->not_suppressed = status_rec_p->not_suppressed;
np->injected = status_rec_p->injected;
np->status_rec_list = NULL;
np->max_pct = fp->max_pct;
add_resource(status_rec_p, rpp, np);
}
}
static void
add_list(status_record_t *status_rec_p, name_list_t *listp,
resource_list_t **glistp)
{
name_list_t *fp, *end;
fp = listp;
end = fp;
while (fp) {
add_resource_list(status_rec_p, fp, glistp);
fp = fp->next;
if (fp == end)
break;
}
}
/*
* add record to rec, fru and asru lists.
*/
static void
catalog_new_record(uurec_t *uurec_p, char *msgid, name_list_t *class,
name_list_t *fru, name_list_t *asru, name_list_t *resource,
name_list_t *serial, boolean_t not_suppressed,
hostid_t *hostid, boolean_t injected, boolean_t dummy_fru)
{
status_record_t *status_rec_p;
status_rec_p = new_record_init(uurec_p, msgid, class, fru, asru,
resource, serial, not_suppressed, hostid, injected);
add_rec_list(status_rec_p, &status_rec_list);
if (status_rec_p->fru && !dummy_fru)
add_list(status_rec_p, status_rec_p->fru, &status_fru_list);
if (status_rec_p->asru)
add_list(status_rec_p, status_rec_p->asru, &status_asru_list);
}
static void
get_serial_no(nvlist_t *nvl, name_list_t **serial_p, uint8_t pct)
{
char *name;
char *serial = NULL;
char **lserial = NULL;
uint64_t serint;
name_list_t *nlp;
int j;
uint_t nelem;
char buf[64];
if (nvlist_lookup_string(nvl, FM_FMRI_SCHEME, &name) == 0) {
if (strcmp(name, FM_FMRI_SCHEME_CPU) == 0) {
if (nvlist_lookup_uint64(nvl, FM_FMRI_CPU_SERIAL_ID,
&serint) == 0) {
(void) snprintf(buf, sizeof (buf), "%llX",
serint);
nlp = alloc_name_list(buf, pct);
(void) merge_name_list(serial_p, nlp, 1);
}
} else if (strcmp(name, FM_FMRI_SCHEME_MEM) == 0) {
if (nvlist_lookup_string_array(nvl,
FM_FMRI_MEM_SERIAL_ID, &lserial, &nelem) == 0) {
nlp = alloc_name_list(lserial[0], pct);
for (j = 1; j < nelem; j++) {
name_list_t *n1lp;
n1lp = alloc_name_list(lserial[j], pct);
(void) merge_name_list(&nlp, n1lp, 1);
}
(void) merge_name_list(serial_p, nlp, 1);
}
} else if (strcmp(name, FM_FMRI_SCHEME_HC) == 0) {
if (nvlist_lookup_string(nvl, FM_FMRI_HC_SERIAL_ID,
&serial) == 0) {
nlp = alloc_name_list(serial, pct);
(void) merge_name_list(serial_p, nlp, 1);
}
}
}
}
static void
extract_record_info(nvlist_t *nvl, name_list_t **class_p,
name_list_t **fru_p, name_list_t **serial_p, name_list_t **resource_p,
name_list_t **asru_p, boolean_t *dummy_fru, uint8_t status)
{
nvlist_t *lfru, *lasru, *rsrc;
name_list_t *nlp;
char *name;
uint8_t lpct = 0;
char *lclass = NULL;
char *label;
(void) nvlist_lookup_uint8(nvl, FM_FAULT_CERTAINTY, &lpct);
if (nvlist_lookup_string(nvl, FM_CLASS, &lclass) == 0) {
nlp = alloc_name_list(lclass, lpct);
(void) merge_name_list(class_p, nlp, 1);
}
if (nvlist_lookup_nvlist(nvl, FM_FAULT_FRU, &lfru) == 0) {
name = get_nvl2str_topo(lfru);
if (name != NULL) {
nlp = alloc_name_list(name, lpct);
nlp->status = status & ~(FM_SUSPECT_UNUSABLE |
FM_SUSPECT_DEGRADED);
free(name);
if (nvlist_lookup_string(nvl, FM_FAULT_LOCATION,
&label) == 0)
nlp->label = strdup(label);
(void) merge_name_list(fru_p, nlp, 1);
}
get_serial_no(lfru, serial_p, lpct);
} else if (nvlist_lookup_nvlist(nvl, FM_FAULT_RESOURCE, &rsrc) != 0) {
/*
* No FRU or resource. But we want to display the repair status
* somehow, so create a dummy FRU field.
*/
*dummy_fru = 1;
nlp = alloc_name_list(dgettext("FMD", "None"), lpct);
nlp->status = status & ~(FM_SUSPECT_UNUSABLE |
FM_SUSPECT_DEGRADED);
(void) merge_name_list(fru_p, nlp, 1);
}
if (nvlist_lookup_nvlist(nvl, FM_FAULT_ASRU, &lasru) == 0) {
name = get_nvl2str_topo(lasru);
if (name != NULL) {
nlp = alloc_name_list(name, lpct);
nlp->status = status & ~(FM_SUSPECT_NOT_PRESENT |
FM_SUSPECT_REPAIRED | FM_SUSPECT_REPLACED |
FM_SUSPECT_ACQUITTED);
free(name);
(void) merge_name_list(asru_p, nlp, 1);
}
get_serial_no(lasru, serial_p, lpct);
}
if (nvlist_lookup_nvlist(nvl, FM_FAULT_RESOURCE, &rsrc) == 0) {
name = get_nvl2str_topo(rsrc);
if (name != NULL) {
nlp = alloc_name_list(name, lpct);
nlp->status = status;
free(name);
if (nvlist_lookup_string(nvl, FM_FAULT_LOCATION,
&label) == 0)
nlp->label = strdup(label);
(void) merge_name_list(resource_p, nlp, 1);
}
}
}
static void
add_fault_record_to_catalog(nvlist_t *nvl, uint64_t sec, char *uuid)
{
char *msgid = "-";
uint_t i, size = 0;
name_list_t *class = NULL, *resource = NULL;
name_list_t *asru = NULL, *fru = NULL, *serial = NULL;
nvlist_t **nva;
uint8_t *ba;
uurec_t *uurec_p;
hostid_t *host;
boolean_t not_suppressed = 1;
boolean_t any_present = 0;
boolean_t injected = 0;
boolean_t dummy_fru = 0;
(void) nvlist_lookup_string(nvl, FM_SUSPECT_DIAG_CODE, &msgid);
(void) nvlist_lookup_uint32(nvl, FM_SUSPECT_FAULT_SZ, &size);
(void) nvlist_lookup_boolean_value(nvl, FM_SUSPECT_MESSAGE,
¬_suppressed);
(void) nvlist_lookup_boolean_value(nvl, FM_SUSPECT_INJECTED, &injected);
if (size != 0) {
(void) nvlist_lookup_nvlist_array(nvl, FM_SUSPECT_FAULT_LIST,
&nva, &size);
(void) nvlist_lookup_uint8_array(nvl, FM_SUSPECT_FAULT_STATUS,
&ba, &size);
for (i = 0; i < size; i++) {
extract_record_info(nva[i], &class, &fru, &serial,
&resource, &asru, &dummy_fru, ba[i]);
if (!(ba[i] & FM_SUSPECT_NOT_PRESENT) &&
(ba[i] & FM_SUSPECT_FAULTY))
any_present = 1;
}
/*
* also suppress if no resources present
*/
if (any_present == 0)
not_suppressed = 0;
}
uurec_p = (uurec_t *)malloc(sizeof (uurec_t));
uurec_p->uuid = strdup(uuid);
uurec_p->sec = sec;
uurec_p->ari_uuid_list = NULL;
uurec_p->event = NULL;
(void) nvlist_dup(nvl, &uurec_p->event, 0);
host = find_hostid(nvl);
catalog_new_record(uurec_p, msgid, class, fru, asru,
resource, serial, not_suppressed, host, injected, dummy_fru);
}
static void
update_asru_state_in_catalog(const char *uuid, const char *ari_uuid)
{
sr_list_t *srp;
uurec_t *uurp;
ari_list_t *ari_list;
srp = status_rec_list;
if (srp) {
for (;;) {
uurp = srp->status_record->uurec;
while (uurp) {
if (strcmp(uuid, uurp->uuid) == 0) {
ari_list = (ari_list_t *)
malloc(sizeof (ari_list_t));
ari_list->ari_uuid = strdup(ari_uuid);
ari_list->next = uurp->ari_uuid_list;
uurp->ari_uuid_list = ari_list;
return;
}
uurp = uurp->next;
}
if (srp->next == status_rec_list)
break;
srp = srp->next;
}
}
}
static void
print_line(char *label, char *buf)
{
char *cp, *ep, *wp;
char c;
int i;
int lsz;
char *padding;
lsz = strlen(label);
padding = malloc(lsz + 1);
for (i = 0; i < lsz; i++)
padding[i] = ' ';
padding[i] = 0;
cp = buf;
ep = buf;
c = *ep;
(void) printf("\n");
while (c) {
i = lsz;
wp = NULL;
while ((c = *ep) != '\0' && (wp == NULL || i < 80)) {
if (c == ' ')
wp = ep;
else if (c == '\n') {
i = 0;
*ep = 0;
do {
ep++;
} while ((c = *ep) != '\0' && c == ' ');
break;
}
ep++;
i++;
}
if (i >= 80 && wp) {
*wp = 0;
ep = wp + 1;
c = *ep;
}
(void) printf("%s%s\n", label, cp);
cp = ep;
label = padding;
}
free(padding);
}
static void
print_dict_info_line(nvlist_t *e, fmd_msg_item_t what, const char *linehdr)
{
char *cp = fmd_msg_getitem_nv(fmadm_msghdl, NULL, e, what);
if (cp) {
print_line(dgettext("FMD", linehdr), cp);
free(cp);
}
}
static void
print_dict_info(nvlist_t *nvl)
{
print_dict_info_line(nvl, FMD_MSG_ITEM_DESC, "Description : ");
print_dict_info_line(nvl, FMD_MSG_ITEM_RESPONSE, "Response : ");
print_dict_info_line(nvl, FMD_MSG_ITEM_IMPACT, "Impact : ");
print_dict_info_line(nvl, FMD_MSG_ITEM_ACTION, "Action : ");
}
static void
print_name(name_list_t *list, char *padding, int *np, int pct, int full)
{
char *name;
name = list->name;
if (list->label) {
(void) printf("%s \"%s\" (%s)", padding, list->label, name);
*np += 1;
} else {
(void) printf("%s %s", padding, name);
*np += 1;
}
if (list->pct && pct > 0 && pct < 100) {
if (list->count > 1) {
if (full) {
(void) printf(" %d @ %s %d%%\n", list->count,
dgettext("FMD", "max"),
list->max_pct);
} else {
(void) printf(" %s %d%%\n",
dgettext("FMD", "max"),
list->max_pct);
}
} else {
(void) printf(" %d%%\n", list->pct);
}
} else {
(void) printf("\n");
}
}
static void
print_asru_status(int status, char *label)
{
char *msg = NULL;
switch (status) {
case 0:
msg = dgettext("FMD", "ok and in service");
break;
case FM_SUSPECT_DEGRADED:
msg = dgettext("FMD", "service degraded, "
"but associated components no longer faulty");
break;
case FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:
msg = dgettext("FMD", "faulted but still "
"providing degraded service");
break;
case FM_SUSPECT_FAULTY:
msg = dgettext("FMD", "faulted but still in service");
break;
case FM_SUSPECT_UNUSABLE:
msg = dgettext("FMD", "out of service, "
"but associated components no longer faulty");
break;
case FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:
msg = dgettext("FMD", "faulted and taken out of service");
break;
default:
break;
}
if (msg) {
(void) printf("%s %s\n", label, msg);
}
}
static void
print_fru_status(int status, char *label)
{
char *msg = NULL;
if (status & FM_SUSPECT_NOT_PRESENT)
msg = dgettext("FMD", "not present");
else if (status & FM_SUSPECT_FAULTY)
msg = dgettext("FMD", "faulty");
else if (status & FM_SUSPECT_REPLACED)
msg = dgettext("FMD", "replaced");
else if (status & FM_SUSPECT_REPAIRED)
msg = dgettext("FMD", "repair attempted");
else if (status & FM_SUSPECT_ACQUITTED)
msg = dgettext("FMD", "acquitted");
else
msg = dgettext("FMD", "removed");
(void) printf("%s %s\n", label, msg);
}
static void
print_rsrc_status(int status, char *label)
{
char *msg = "";
if (status & FM_SUSPECT_NOT_PRESENT)
msg = dgettext("FMD", "not present");
else if (status & FM_SUSPECT_FAULTY) {
if (status & FM_SUSPECT_DEGRADED)
msg = dgettext("FMD",
"faulted but still providing degraded service");
else if (status & FM_SUSPECT_UNUSABLE)
msg = dgettext("FMD",
"faulted and taken out of service");
else
msg = dgettext("FMD", "faulted but still in service");
} else if (status & FM_SUSPECT_REPLACED)
msg = dgettext("FMD", "replaced");
else if (status & FM_SUSPECT_REPAIRED)
msg = dgettext("FMD", "repair attempted");
else if (status & FM_SUSPECT_ACQUITTED)
msg = dgettext("FMD", "acquitted");
else
msg = dgettext("FMD", "removed");
(void) printf("%s %s\n", label, msg);
}
static void
print_name_list(name_list_t *list, char *label,
int limit, int pct, void (func1)(int, char *), int full)
{
char *name;
char *padding;
int i, j, l, n;
name_list_t *end = list;
l = strlen(label);
padding = malloc(l + 1);
for (i = 0; i < l; i++)
padding[i] = ' ';
padding[l] = 0;
(void) printf("%s", label);
name = list->name;
if (list->label)
(void) printf(" \"%s\" (%s)", list->label, name);
else
(void) printf(" %s", name);
if (list->pct && pct > 0 && pct < 100) {
if (list->count > 1) {
if (full) {
(void) printf(" %d @ %s %d%%\n", list->count,
dgettext("FMD", "max"), list->max_pct);
} else {
(void) printf(" %s %d%%\n",
dgettext("FMD", "max"), list->max_pct);
}
} else {
(void) printf(" %d%%\n", list->pct);
}
} else {
(void) printf("\n");
}
if (func1)
func1(list->status, padding);
n = 1;
j = 0;
while ((list = list->next) != end) {
if (limit == 0 || n < limit) {
print_name(list, padding, &n, pct, full);
if (func1)
func1(list->status, padding);
} else
j++;
}
if (j == 1) {
print_name(list->prev, padding, &n, pct, full);
} else if (j > 1) {
(void) printf("%s... %d %s\n", padding, j,
dgettext("FMD", "more entries suppressed,"
" use -v option for full list"));
}
free(padding);
}
static int
asru_same_status(name_list_t *list)
{
name_list_t *end = list;
int status = list->status;
while ((list = list->next) != end) {
if (status == -1) {
status = list->status;
continue;
}
if (list->status != -1 && status != list->status) {
status = -1;
break;
}
}
return (status);
}
static int
serial_in_fru(name_list_t *fru, name_list_t *serial)
{
name_list_t *sp = serial;
name_list_t *fp;
int nserial = 0;
int found = 0;
char buf[128];
while (sp) {
fp = fru;
nserial++;
(void) snprintf(buf, sizeof (buf), "serial=%s", sp->name);
buf[sizeof (buf) - 1] = 0;
while (fp) {
if (strstr(fp->name, buf) != NULL) {
found++;
break;
}
fp = fp->next;
if (fp == fru)
break;
}
sp = sp->next;
if (sp == serial)
break;
}
return (found == nserial ? 1 : 0);
}
static void
print_sup_record(status_record_t *srp, int opt_i, int full)
{
char buf[32];
uurec_t *uurp = srp->uurec;
int n, j, k, max;
int status;
ari_list_t *ari_list;
n = 0;
max = max_fault;
if (max < 0) {
max = 0;
}
j = max / 2;
max -= j;
k = srp->nrecs - max;
while ((uurp = uurp->next) != NULL) {
if (full || n < j || n >= k || max_fault == 0 ||
srp->nrecs == max_fault+1) {
if (opt_i) {
ari_list = uurp->ari_uuid_list;
while (ari_list) {
(void) printf("%-15s %s\n",
format_date(buf, sizeof (buf),
uurp->sec), ari_list->ari_uuid);
ari_list = ari_list->next;
}
} else {
(void) printf("%-15s %s\n",
format_date(buf, sizeof (buf), uurp->sec),
uurp->uuid);
}
} else if (n == j)
(void) printf("... %d %s\n", srp->nrecs - max_fault,
dgettext("FMD", "more entries suppressed"));
n++;
}
(void) printf("\n");
if (srp->host) {
(void) printf("%s %s", dgettext("FMD", "Host :"),
srp->host->server);
if (srp->host->domain)
(void) printf("\t%s %s", dgettext("FMD",
"Domain :"), srp->host->domain);
(void) printf("\n%s %s", dgettext("FMD", "Platform :"),
srp->host->platform);
(void) printf("\t%s %s", dgettext("FMD", "Chassis_id :"),
srp->host->chassis ? srp->host->chassis : "");
(void) printf("\n%s %s\n\n", dgettext("FMD", "Product_sn :"),
srp->host->product_sn ? srp->host->product_sn : "");
}
if (srp->class)
print_name_list(srp->class,
dgettext("FMD", "Fault class :"), 0, srp->class->pct,
NULL, full);
if (srp->asru) {
status = asru_same_status(srp->asru);
if (status != -1) {
print_name_list(srp->asru,
dgettext("FMD", "Affects :"),
full ? 0 : max_display, 0, NULL, full);
print_asru_status(status, " ");
} else
print_name_list(srp->asru,
dgettext("FMD", "Affects :"),
full ? 0 : max_display, 0, print_asru_status, full);
}
if (full || srp->fru == NULL || srp->asru == NULL) {
if (srp->resource) {
status = asru_same_status(srp->resource);
if (status != -1) {
print_name_list(srp->resource,
dgettext("FMD", "Problem in :"),
full ? 0 : max_display, 0, NULL, full);
print_rsrc_status(status, " ");
} else
print_name_list(srp->resource,
dgettext("FMD", "Problem in :"),
full ? 0 : max_display, 0,
print_rsrc_status, full);
}
}
if (srp->fru) {
status = asru_same_status(srp->fru);
if (status != -1) {
print_name_list(srp->fru, dgettext("FMD",
"FRU :"), 0,
srp->fru->pct == 100 ? 100 : srp->fru->max_pct,
NULL, full);
print_fru_status(status, " ");
} else
print_name_list(srp->fru, dgettext("FMD",
"FRU :"), 0,
srp->fru->pct == 100 ? 100 : srp->fru->max_pct,
print_fru_status, full);
}
if (srp->serial && !serial_in_fru(srp->fru, srp->serial) &&
!serial_in_fru(srp->asru, srp->serial)) {
print_name_list(srp->serial, dgettext("FMD", "Serial ID. :"),
0, 0, NULL, full);
}
print_dict_info(srp->uurec->event);
(void) printf("\n");
}
static void
print_status_record(status_record_t *srp, int summary, int opt_i, int full)
{
char buf[32];
uurec_t *uurp = srp->uurec;
static int header = 0;
char *head;
ari_list_t *ari_list;
if (!summary || !header) {
if (opt_i) {
head = "--------------- "
"------------------------------------ "
"-------------- ---------\n"
"TIME CACHE-ID"
" MSG-ID"
" SEVERITY\n--------------- "
"------------------------------------ "
" -------------- ---------";
} else {
head = "--------------- "
"------------------------------------ "
"-------------- ---------\n"
"TIME EVENT-ID"
" MSG-ID"
" SEVERITY\n--------------- "
"------------------------------------ "
" -------------- ---------";
}
(void) printf("%s\n", dgettext("FMD", head));
header = 1;
}
if (opt_i) {
ari_list = uurp->ari_uuid_list;
while (ari_list) {
(void) printf("%-15s %-37s %-14s %-9s %s\n",
format_date(buf, sizeof (buf), uurp->sec),
ari_list->ari_uuid, srp->msgid, srp->severity,
srp->injected ? dgettext("FMD", "injected") : "");
ari_list = ari_list->next;
}
} else {
(void) printf("%-15s %-37s %-14s %-9s %s\n",
format_date(buf, sizeof (buf), uurp->sec),
uurp->uuid, srp->msgid, srp->severity,
srp->injected ? dgettext("FMD", "injected") : "");
}
if (!summary)
print_sup_record(srp, opt_i, full);
}
static void
print_catalog(int summary, int opt_a, int full, int opt_i, int page_feed)
{
status_record_t *srp;
sr_list_t *slp;
slp = status_rec_list;
if (slp) {
for (;;) {
srp = slp->status_record;
if (opt_a || srp->not_suppressed) {
if (page_feed)
(void) printf("\f\n");
print_status_record(srp, summary, opt_i, full);
}
if (slp->next == status_rec_list)
break;
slp = slp->next;
}
}
}
static name_list_t *
find_fru(status_record_t *srp, char *resource)
{
name_list_t *rt = NULL;
name_list_t *fru = srp->fru;
while (fru) {
if (strcmp(resource, fru->name) == 0) {
rt = fru;
break;
}
fru = fru->next;
if (fru == srp->fru)
break;
}
return (rt);
}
static void
print_fru_line(name_list_t *fru, char *uuid)
{
if (fru->pct == 100) {
(void) printf("%s %d %s %d%%\n", uuid, fru->count,
dgettext("FMD", "suspects in this FRU total certainty"),
100);
} else {
(void) printf("%s %d %s %d%%\n", uuid, fru->count,
dgettext("FMD", "suspects in this FRU max certainty"),
fru->max_pct);
}
}
static void
print_fru(int summary, int opt_a, int opt_i, int page_feed)
{
resource_list_t *tp = status_fru_list;
status_record_t *srp;
sr_list_t *slp, *end;
uurec_t *uurp;
name_list_t *fru;
int status;
ari_list_t *ari_list;
while (tp) {
if (opt_a || tp->not_suppressed) {
if (page_feed)
(void) printf("\f\n");
if (!summary)
(void) printf("-----------------------------"
"---------------------------------------"
"----------\n");
slp = tp->status_rec_list;
end = slp;
do {
srp = slp->status_record;
if (!srp->not_suppressed) {
slp = slp->next;
continue;
}
fru = find_fru(srp, tp->resource);
if (fru) {
if (fru->label)
(void) printf("\"%s\" (%s) ",
fru->label, fru->name);
else
(void) printf("%s ",
fru->name);
break;
}
slp = slp->next;
} while (slp != end);
slp = tp->status_rec_list;
end = slp;
status = 0;
do {
srp = slp->status_record;
if (!srp->not_suppressed) {
slp = slp->next;
continue;
}
fru = srp->fru;
while (fru) {
if (strcmp(tp->resource,
fru->name) == 0)
status |= fru->status;
fru = fru->next;
if (fru == srp->fru)
break;
}
slp = slp->next;
} while (slp != end);
if (status & FM_SUSPECT_NOT_PRESENT)
(void) printf(dgettext("FMD", "not present"));
else if (status & FM_SUSPECT_FAULTY)
(void) printf(dgettext("FMD", "faulty"));
else if (status & FM_SUSPECT_REPLACED)
(void) printf(dgettext("FMD", "replaced"));
else if (status & FM_SUSPECT_REPAIRED)
(void) printf(dgettext("FMD",
"repair attempted"));
else if (status & FM_SUSPECT_ACQUITTED)
(void) printf(dgettext("FMD", "acquitted"));
else
(void) printf(dgettext("FMD", "removed"));
if (tp->injected)
(void) printf(dgettext("FMD", " injected\n"));
else
(void) printf(dgettext("FMD", "\n"));
slp = tp->status_rec_list;
end = slp;
do {
srp = slp->status_record;
if (!srp->not_suppressed) {
slp = slp->next;
continue;
}
uurp = srp->uurec;
fru = find_fru(srp, tp->resource);
if (fru) {
if (opt_i) {
ari_list = uurp->ari_uuid_list;
while (ari_list) {
print_fru_line(fru,
ari_list->ari_uuid);
ari_list =
ari_list->next;
}
} else {
print_fru_line(fru, uurp->uuid);
}
}
slp = slp->next;
} while (slp != end);
if (!summary) {
slp = tp->status_rec_list;
end = slp;
do {
srp = slp->status_record;
if (!srp->not_suppressed) {
slp = slp->next;
continue;
}
if (srp->serial &&
!serial_in_fru(srp->fru,
srp->serial)) {
print_name_list(srp->serial,
dgettext("FMD",
"Serial ID. :"),
0, 0, NULL, 1);
break;
}
slp = slp->next;
} while (slp != end);
}
}
tp = tp->next;
if (tp == status_fru_list)
break;
}
}
static void
print_asru(int opt_a)
{
resource_list_t *tp = status_asru_list;
status_record_t *srp;
sr_list_t *slp, *end;
char *msg;
int status;
name_list_t *asru;
while (tp) {
if (opt_a || tp->not_suppressed) {
status = 0;
slp = tp->status_rec_list;
end = slp;
do {
srp = slp->status_record;
if (!srp->not_suppressed) {
slp = slp->next;
continue;
}
asru = srp->asru;
while (asru) {
if (strcmp(tp->resource,
asru->name) == 0)
status |= asru->status;
asru = asru->next;
if (asru == srp->asru)
break;
}
slp = slp->next;
} while (slp != end);
switch (status) {
case 0:
msg = dgettext("FMD", "ok");
break;
case FM_SUSPECT_DEGRADED:
msg = dgettext("FMD", "degraded");
break;
case FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:
msg = dgettext("FMD", "degraded");
break;
case FM_SUSPECT_FAULTY:
msg = dgettext("FMD", "degraded");
break;
case FM_SUSPECT_UNUSABLE:
msg = dgettext("FMD", "unknown");
break;
case FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:
msg = dgettext("FMD", "faulted");
break;
default:
msg = "";
break;
}
(void) printf("%-69s %s", tp->resource, msg);
if (tp->injected)
(void) printf(dgettext("FMD", " injected\n"));
else
(void) printf(dgettext("FMD", "\n"));
}
tp = tp->next;
if (tp == status_asru_list)
break;
}
}
static int
uuid_in_list(char *uuid, uurec_select_t *uurecp)
{
while (uurecp) {
if (strcmp(uuid, uurecp->uuid) == 0)
return (1);
uurecp = uurecp->next;
}
return (0);
}
static int
dfault_rec(const fmd_adm_caseinfo_t *acp, void *arg)
{
int64_t *diag_time;
uint_t nelem;
int rt = 0;
char *uuid = "-";
uurec_select_t *uurecp = (uurec_select_t *)arg;
if (nvlist_lookup_int64_array(acp->aci_event, FM_SUSPECT_DIAG_TIME,
&diag_time, &nelem) == 0 && nelem >= 2) {
(void) nvlist_lookup_string(acp->aci_event, FM_SUSPECT_UUID,
&uuid);
if (uurecp == NULL || uuid_in_list(uuid, uurecp))
add_fault_record_to_catalog(acp->aci_event, *diag_time,
uuid);
} else {
rt = -1;
}
return (rt);
}
/*ARGSUSED*/
static int
dstatus_rec(const fmd_adm_rsrcinfo_t *ari, void *unused)
{
update_asru_state_in_catalog(ari->ari_case, ari->ari_uuid);
return (0);
}
static int
get_cases_from_fmd(fmd_adm_t *adm, uurec_select_t *uurecp, int opt_i)
{
int rt = FMADM_EXIT_SUCCESS;
/*
* These calls may fail with Protocol error if message payload is
* too big
*/
if (fmd_adm_case_iter(adm, NULL, dfault_rec, uurecp) != 0)
die("failed to get case list from fmd");
if (opt_i && fmd_adm_rsrc_iter(adm, 1, dstatus_rec, NULL) != 0)
die("failed to get case status from fmd");
return (rt);
}
/*
* fmadm faulty command
*
* -a show hidden fault records
* -f show faulty fru's
* -g force grouping of similar faults on the same fru
* -n number of fault records to display
* -p pipe output through pager
* -r show faulty asru's
* -s print summary of first fault
* -u print listed uuid's only
* -v full output
*/
int
cmd_faulty(fmd_adm_t *adm, int argc, char *argv[])
{
int opt_a = 0, opt_v = 0, opt_p = 0, opt_s = 0, opt_r = 0, opt_f = 0;
int opt_i = 0;
char *pager;
FILE *fp;
int rt, c, stat;
uurec_select_t *tp;
uurec_select_t *uurecp = NULL;
while ((c = getopt(argc, argv, "afgin:prsu:v")) != EOF) {
switch (c) {
case 'a':
opt_a++;
break;
case 'f':
opt_f++;
break;
case 'g':
opt_g++;
break;
case 'i':
opt_i++;
break;
case 'n':
max_fault = atoi(optarg);
break;
case 'p':
opt_p++;
break;
case 'r':
opt_r++;
break;
case 's':
opt_s++;
break;
case 'u':
tp = (uurec_select_t *)malloc(sizeof (uurec_select_t));
tp->uuid = optarg;
tp->next = uurecp;
uurecp = tp;
opt_a = 1;
break;
case 'v':
opt_v++;
break;
default:
return (FMADM_EXIT_USAGE);
}
}
if (optind < argc)
return (FMADM_EXIT_USAGE);
if ((fmadm_msghdl = fmd_msg_init(NULL, FMD_MSG_VERSION)) == NULL)
return (FMADM_EXIT_ERROR);
rt = get_cases_from_fmd(adm, uurecp, opt_i);
if (opt_p) {
if ((pager = getenv("PAGER")) == NULL)
pager = "/usr/bin/more";
fp = popen(pager, "w");
if (fp == NULL) {
rt = FMADM_EXIT_ERROR;
opt_p = 0;
} else {
(void) dup2(fileno(fp), 1);
setbuf(stdout, NULL);
(void) fclose(fp);
}
}
max_display = max_fault;
if (opt_f)
print_fru(opt_s, opt_a, opt_i, opt_p && !opt_s);
if (opt_r)
print_asru(opt_a);
if (opt_f == 0 && opt_r == 0)
print_catalog(opt_s, opt_a, opt_v, opt_i, opt_p && !opt_s);
fmd_msg_fini(fmadm_msghdl);
if (topo_handle)
topo_close(topo_handle);
if (opt_p) {
(void) fclose(stdout);
(void) wait(&stat);
}
return (rt);
}
int
cmd_flush(fmd_adm_t *adm, int argc, char *argv[])
{
int i, status = FMADM_EXIT_SUCCESS;
if (argc < 2 || (i = getopt(argc, argv, "")) != EOF)
return (FMADM_EXIT_USAGE);
for (i = 1; i < argc; i++) {
if (fmd_adm_rsrc_flush(adm, argv[i]) != 0) {
warn("failed to flush %s", argv[i]);
status = FMADM_EXIT_ERROR;
} else
note("flushed resource history for %s\n", argv[i]);
}
return (status);
}
int
cmd_repair(fmd_adm_t *adm, int argc, char *argv[])
{
int err;
if (getopt(argc, argv, "") != EOF)
return (FMADM_EXIT_USAGE);
if (argc - optind != 1)
return (FMADM_EXIT_USAGE);
/*
* argument could be a uuid, an fmri (asru, fru or resource)
* or a label. Try uuid first, If that fails try the others.
*/
err = fmd_adm_case_repair(adm, argv[optind]);
if (err != 0)
err = fmd_adm_rsrc_repaired(adm, argv[optind]);
if (err != 0)
die("failed to record repair to %s", argv[optind]);
note("recorded repair to %s\n", argv[optind]);
return (FMADM_EXIT_SUCCESS);
}
int
cmd_repaired(fmd_adm_t *adm, int argc, char *argv[])
{
int err;
if (getopt(argc, argv, "") != EOF)
return (FMADM_EXIT_USAGE);
if (argc - optind != 1)
return (FMADM_EXIT_USAGE);
/*
* argument could be an fmri (asru, fru or resource) or a label.
*/
err = fmd_adm_rsrc_repaired(adm, argv[optind]);
if (err != 0)
die("failed to record repair to %s", argv[optind]);
note("recorded repair to of %s\n", argv[optind]);
return (FMADM_EXIT_SUCCESS);
}
int
cmd_replaced(fmd_adm_t *adm, int argc, char *argv[])
{
int err;
if (getopt(argc, argv, "") != EOF)
return (FMADM_EXIT_USAGE);
if (argc - optind != 1)
return (FMADM_EXIT_USAGE);
/*
* argument could be an fmri (asru, fru or resource) or a label.
*/
err = fmd_adm_rsrc_replaced(adm, argv[optind]);
if (err != 0)
die("failed to record replacement of %s", argv[optind]);
note("recorded replacement of %s\n", argv[optind]);
return (FMADM_EXIT_SUCCESS);
}
int
cmd_acquit(fmd_adm_t *adm, int argc, char *argv[])
{
int err;
if (getopt(argc, argv, "") != EOF)
return (FMADM_EXIT_USAGE);
if (argc - optind != 1 && argc - optind != 2)
return (FMADM_EXIT_USAGE);
/*
* argument could be a uuid, an fmri (asru, fru or resource)
* or a label. Or it could be a uuid and an fmri or label.
*/
if (argc - optind == 2) {
err = fmd_adm_rsrc_acquit(adm, argv[optind], argv[optind + 1]);
if (err != 0)
err = fmd_adm_rsrc_acquit(adm, argv[optind + 1],
argv[optind]);
} else {
err = fmd_adm_case_acquit(adm, argv[optind]);
if (err != 0)
err = fmd_adm_rsrc_acquit(adm, argv[optind], "");
}
if (err != 0)
die("failed to record acquital of %s", argv[optind]);
note("recorded acquital of %s\n", argv[optind]);
return (FMADM_EXIT_SUCCESS);
}
|