File: xmeter.c

package info (click to toggle)
xmeter 1.15-5
  • links: PTS
  • area: main
  • in suites: sarge, woody
  • size: 148 kB
  • ctags: 681
  • sloc: ansic: 1,114; makefile: 736
file content (1523 lines) | stat: -rw-r--r-- 43,372 bytes parent folder | download | duplicates (2)
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
/*
 * xmeter.c - Display histogram of rstat(3) output for multiple hosts.
 *
 * Copyright (c) 1991, Bob Schwartzkopf
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation for any purpose is hereby granted without fee, provided
 * that the above copyright notice appear in all copies and that both the
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of the RAND Corporation not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.  The RAND Corporation
 * makes no representations about the suitability of this software for
 * any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * The X Consortium, and any party obtaining a copy of these files from
 * the X Consortium, directly or indirectly, is granted, free of charge, a
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 * nonexclusive right and license to deal in this software and
 * documentation files (the "Software"), including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons who receive
 * copies from any such party to do so.  This license includes without
 * limitation a license to do the foregoing actions under any patents of
 * the party supplying this software to the X Consortium.
 *
 * Author: Bob Schwartzkopf, The RAND Corporation
 *
 * Suggestions for improvements and bug fixes can be sent to "bobs@rand.org".
 * As my schedule permits I'll try to incorporate them.
 */

#ifndef lint
static char	*RCSid="$Header: /net/src/rand/bin/xmeter/RCS/xmeter.c,v 1.19 1994/05/25 01:01:08 bobs Exp $";
#endif lint

/*
 * $Log: xmeter.c,v $
 * Revision 1.19  1994/05/25  01:01:08  bobs
 * Add X copyright.
 *
 * Revision 1.18  1994/02/12  02:46:40  bobs
 * Port to solaris 2.
 *
 * Revision 1.17  1994/02/10  20:54:17  bobs
 * Fixed bug in quit() that caused core dumps.
 * Removed bcopy() call in newshmeter().
 *
 * Revision 1.16  1993/12/09  20:10:26  bobs
 * Patches 12 and 13 (see README for details).
 *
 * Revision 1.15  1993/08/18  01:42:33  bobs
 * Fixed bug in fdisks(), dk_xfer dimensioned is 4.
 * Include <X11/Xos.h>, remove some other includes and external declarations.
 * Changed vfork to vork.
 * Don't destroy popup widget in popdownscale if it's null.
 * Include <sys/dk.h> under hpux.
 *
 * Revision 1.14  1993/05/25  00:06:27  bobs
 * Use sigset(3) under SVR4.
 *
 * Revision 1.13  1993/01/25  22:32:18  bobs
 * Redo previous 2 edits.
 *
 * Revision 1.12  1993/01/19  00:24:22  bobs
 * Have child processes exit if parent has gone away.
 *
 * Revision 1.11  1993/01/19  00:12:33  bobs
 * Kill children on INT signal too.
 *
 * Revision 1.10  1993/01/19  00:08:56  bobs
 * Kill child processes on TERM signal.
 *
 * Revision 1.9  1992/05/21  00:11:17  bobs
 * Add option to display current value in label.
 * Add quit option to stat menu.
 * Redo application resource handling in more portable way.
 * Use complete hostname for comparisons when shortnames has been specified.
 * Redo child process cleanup code in a hopefully more portable way.
 * Add stat name as fourth arg to alert programs.
 * Fix divide by zero problem in functions that return stat values.
 *
 * Revision 1.8  1991/09/16  21:01:04  bobs
 * Fix bugs handling down hosts.
 * Add composite stats swap, disks, page and pkts.
 * Add popup window with version number, scale and downtime (if applicable).
 *
 * Revision 1.7  1991/06/03  22:21:43  root
 * Add -v option to print xmeter version number.
 * Add defstat option.
 * Add code to clear stripchart when watched stat is changed.
 * When host is down fork process to wait for it to come up instead of
 * having toplevel process wait for timeouts.
 *
 * Revision 1.6  90/09/28  20:32:34  root
 * Add explicit closes of sockets back in, apparently some versions of
 * clnt_destroy() don't do it automatically.
 * 
 * Revision 1.5  90/09/18  20:47:03  root
 * Allow user specification of foreground colors.
 * 
 * Revision 1.4  90/08/20  14:18:01  root
 * Added menus, column and row options, background bitmaps, and user
 * specifiable programs to be invoked when graphs change state.
 * 
 * Revision 1.3  90/06/07  16:17:06  bobs
 * Removed "retries".
 * Changed name of paned widgets to host name displayed in that widget.
 * Used actual time between rstat calls instead of "update" interval in
 * computing rates in functions that return values to stripchart widgets.
 * Removed "ost" variable, rewrote functions that return values to
 * stripcharts.
 * Fixed bug in fsys, fcpu and fuser that could cause divide by 0 errors.
 * Fixed rpc timeout handling in getmeter and getport.
 * Use RSTATVERS_TIME instead of RSTATVERS, which isn't defined in SunOS 4.1.
 * 
 * Revision 1.2  90/05/04  12:31:52  bobs
 * Fix memory leak in getport().  Wasn't freeing resources allocated by
 * clntudp_create when clnt_call failed.  Also removed explicit calls
 * to close sockets since clnt_destroy() does this.
 * 
 * Revision 1.1  90/04/30  14:33:59  bobs
 * Initial revision
 * 
 */

#include <stdio.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>
#include <netdb.h>
#define PORTMAP		/* Get right function declarations on Solaris 2	*/
#include <rpc/rpc.h>
#include <rpc/pmap_prot.h>
#include <rpcsvc/rstat.h>
/*
 * We need the definitions of CP_USER, CP_NICE and CP_SYS.  Some systems
 * declare them in <sys/dk.h>, which may or may not be included by
 * <rpcsvc/rstat.h>, others define them in <rpcsvc/rstat.h>, and others 
 * don't seem to define them anywhere.  To simplify things I'm just
 * going to define them here if I didn't get them from <rpcsvc/rstat.h>.
 * If they ever change this is going to break.
 */
#ifndef CP_USER
#define CP_USER		0
#define CP_NICE		1
#define CP_SYS		2
#endif
#ifndef CPUSTATES
#ifdef RSTAT_CPUSTATES
#define CPUSTATES	RSTAT_CPUSTATES
#else
#define CPUSTATES	4
#endif
#endif
#include <X11/Xos.h>
#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xatom.h>
#include <X11/Shell.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/StripChart.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xmu/Xmu.h>
#include "patchlevel.h"

#define MAJORVERSION	1
#define DMSG		"down"
#define OK		0
#define WARN		1
#define ERROR		2
#define FATAL		3
#define MAXBACKS	(FATAL+1)
#define STATMENU	"statmenu"
#ifndef FSCALE
#define FSCALE		(1 << 8)
#endif

/*
 * There is one METER structure per strip chart.  If multiple stats
 * on a single host are being watched we save rstatd calls by sharing
 * part of this structure (SHMETER) between the different meters watching
 * the same host.
 */
typedef struct _shmeter {
    char		*name;	/* Host name				*/
    char		*label;	/* Displayed host name			*/
    struct sockaddr_in	addr;
    CLIENT		*clnt;
    int			s;	/* Socket				*/
    struct statstime	st[2];	/* Keep last 2 stats to compute diffs	*/
    int			idx;	/* Index into st array			*/
    int			refcnt;	/* Meters sharing this structure	*/
    int			curcnt;	/* Meters who've displayed current data	*/
    int			first;	/* TRUE when only 1 rstat has been done	*/
    int			pid;	/* Pid of proc waiting for host		*/
    time_t		when;	/* Time we first notice host down	*/
    struct _shmeter	*nxt;	/* Link these together			*/
} SHMETER;

typedef struct _meter {
    char		*label;
    int			stat;		/* Which stat to watch		*/
    Widget		pdwidget;	/* Paned widget			*/
    Widget		mbwidget;	/* MenuButton widget		*/
    Widget		scwidget;	/* StripChart widget		*/
    int			oldstate;	/* Save state so know when to	*/
					/* change backgrounds		*/
    SHMETER		*sh;		/* Shared info			*/
    Pixmap		pm;		/* Current background pixmap	*/
    int			oldjumpscroll;	/* Old jumpscroll value		*/
    struct _meter	*nxt;
} METER;

/*
 * There is a STATDATA structure for each statistic that can be monitored.
 */
typedef struct {
    int		(*val)();	/* Function that computes this stat	*/
    char	*name;		/* Name of stat for menu widget		*/
} STATDATA;

#if defined(SVR4) || defined(sgi)
#define SIGTYPE	void
#else
#define SIGTYPE	int
#endif

int		getstatus();
int		changestat();
int		selecthost();
SIGTYPE		freechild();
METER		*initmeter();
SHMETER		*newshmeter();
int		fcoll(), fcpu(), fierr(), fintr(), fipkt(), fload();
int		foerr(), fopkt(), fpage(), fpgpgin(), fpgpgout();
int		fpswpin(), fpswpout(), fswt(), fsys(), fuser();
int		fpkts(), fdisks(), fswap(), ferr();
void		popupscale();
void		popdownscale();
void		quit();
char		*mystrdup();

static STATDATA		sd[] = {
#define S_COLL		0
  {fcoll, "coll"},
#define S_CPU		S_COLL+1
  {fcpu, "cpu"},
#define S_DISKS		S_CPU+1
  {fdisks, "disks"},
#define S_ERR		S_DISKS+1
  {ferr, "err"},
#define S_IERR		S_ERR+1
  {fierr, "ierr"},
#define S_INTR		S_IERR+1
  {fintr, "intr"},
#define S_IPKT		S_INTR+1
  {fipkt, "ipkt"},
#define S_LOAD		S_IPKT+1
  {fload, "load"},
#define S_OERR		S_LOAD+1
  {foerr, "oerr"},
#define S_OPKT		S_OERR+1
  {fopkt, "opkt"},
#define S_PAGE		S_OPKT+1
  {fpage, "page"},
#define S_PGPGIN	S_PAGE+1
  {fpgpgin, "pgpgin"},
#define S_PGPGOUT	S_PGPGIN+1
  {fpgpgout, "pgpgout"},
#define S_PKTS		S_PGPGOUT+1
  {fpkts, "pkts"},
#define S_PSWPIN	S_PKTS+1
  {fpswpin, "pswpin"},
#define S_PSWPOUT	S_PSWPIN+1
  {fpswpout, "pswpout"},
#define S_SWAP		S_PSWPOUT+1
  {fswap, "swap"},
#define S_SWT		S_SWAP+1
  {fswt, "swt"},
#define S_SYS		S_SWT+1
  {fsys, "sys"},
#define S_USER		S_SYS+1
  {fuser, "user"},
};
#define DEFSTATNAME	"load"
#define MAXSTAT		(sizeof(sd) / sizeof(STATDATA))
#define MAXSTATNAME	10		/* Max length of stat name	*/
#define MAXSTATVALUE	10		/* Max length of stat value	*/

/*
 * xmeter supports 4 background colors and pixmaps that it will switch
 * between as a meter changes state, called OK, WARN, ERROR and FATAL.
 * The background bitmaps are stored in BITMAP structures.
 */
typedef struct {
  int		w;				/* Width of bitmap	*/
  int		h;				/* Height of bitmap	*/
  Pixmap	bm;				/* Bitmap		*/
} BITMAP;

/*
 * Application specific resources are stored in an APPRESOURCES structure.
 **/
typedef struct {
  Pixel			back[MAXBACKS];		/* Meter backgrounds	*/
  Pixel			bd[MAXBACKS];		/* Meter borders	*/
  BITMAP		bitmap[MAXBACKS];	/* Stripchart bg pixmaps*/
  int			columns;		/* Columns to display	*/
  String		defStat;		/* Default stat		*/
  Boolean		displayValue;		/* Show value in label	*/
  int			errorLevel;		/* Error level		*/
  Pixel			fore[MAXBACKS];		/* Meter foregrounds	*/
  Pixel			hl[MAXBACKS];		/* Meter highlights	*/
  Pixel			ibd[MAXBACKS];		/* Meter internal bds	*/
  Pixel			lback[MAXBACKS];	/* Label backgrounds	*/
  Pixel			lfore[MAXBACKS];	/* Label foregrounds	*/
  String		prog[MAXBACKS];		/* Alert programs	*/
  int			retries;		/* RPC retries		*/
  int			rows;			/* Rows	to display	*/
  int			scales[MAXSTAT];	/* Scale for each stat	*/
  Boolean		shortName;		/* Short hostname flag	*/
  int			timeout;		/* RPC timeout		*/
  int			warnLevel;		/* Warning level	*/
} APPRESOURCES;

/*
 * Global variables.
 */
static char		*progname;
static Pixmap		bitmapdef = XtUnspecifiedPixmap;
static struct timeval	ptto = {0, 0};		/* RPC timeout interval	*/
static struct timeval	tto = {0, 0};		/* Total timeout	*/
static SHMETER		*shmeters = NULL;	/* List of SHMETERs	*/
static METER		*selected;		/* Current meter	*/
static METER		*meterlist = NULL;	/* List of METERs	*/
static int		loadscaledef = FSCALE;	/* Scale for load ave.	*/
static Widget		popup;		/* Used by pop[up|down]scale	*/
static APPRESOURCES	ar;		/* App specific resources	*/

#define offset(f)	XtOffsetOf(APPRESOURCES, f)
static XtResource	resources[] = {
    {"columns", "Columns", XtRInt, sizeof(int),
	offset(columns), XtRString, "0"},
    {"defStat", "DefStat", XtRString, sizeof(char *),
	offset(defStat), XtRString, DEFSTATNAME},
    {"displayValue", "DisplayValue", XtRBoolean, sizeof(Boolean),
	offset(displayValue), XtRString, "False"},
    {"errorBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(back[ERROR]), XtRString, XtDefaultBackground},
    {"errorBd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(bd[ERROR]), XtRString, XtDefaultForeground},
    {"errorBitmap", XtCBitmap, XtRBitmap, sizeof(Pixmap),
	offset(bitmap[ERROR].bm), XtRBitmap,(caddr_t) &bitmapdef},
    {"errorFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(fore[ERROR]), XtRString, XtDefaultForeground},
    {"errorHl", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(hl[ERROR]), XtRString, XtDefaultForeground},
    {"errorIbd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(ibd[ERROR]), XtRString, XtDefaultForeground},
    {"errorLevel", "ErrorLevel", XtRInt, sizeof(int),
	offset(errorLevel), XtRString, "6"},
    {"errorProg", "Program", XtRString, sizeof(char *),
	offset(prog[ERROR]), XtRString, NULL},
    {"fatalBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(back[FATAL]), XtRString, XtDefaultBackground},
    {"fatalBd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(bd[FATAL]), XtRString, XtDefaultForeground},
    {"fatalBitmap", XtCBitmap, XtRBitmap, sizeof(Pixmap),
	offset(bitmap[FATAL].bm), XtRBitmap,(caddr_t) &bitmapdef},
    {"fatalFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(fore[FATAL]), XtRString, XtDefaultForeground},
    {"fatalHl", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(hl[FATAL]), XtRString, XtDefaultForeground},
    {"fatalIbd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(ibd[FATAL]), XtRString, XtDefaultForeground},
    {"fatalProg", "Program", XtRString, sizeof(char *),
	offset(prog[FATAL]), XtRString, NULL},
    {"lErrorBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(lback[ERROR]), XtRString, XtDefaultBackground},
    {"lErrorFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(lfore[ERROR]), XtRString, XtDefaultForeground},
    {"lFatalBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(lback[FATAL]), XtRString, XtDefaultBackground},
    {"lFatalFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(lfore[FATAL]), XtRString, XtDefaultForeground},
    {"lOkBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(lback[OK]), XtRString, XtDefaultBackground},
    {"lOkFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(lfore[OK]), XtRString, XtDefaultForeground},
    {"lWarnBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(lback[WARN]), XtRString, XtDefaultBackground},
    {"lWarnFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(lfore[WARN]), XtRString, XtDefaultForeground},
    {"okBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(back[OK]), XtRString, XtDefaultBackground},
    {"okBd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(bd[OK]), XtRString, XtDefaultForeground},
    {"okBitmap", XtCBitmap, XtRBitmap, sizeof(Pixmap),
	offset(bitmap[OK].bm), XtRBitmap, (caddr_t) &bitmapdef},
    {"okFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(fore[OK]), XtRString, XtDefaultForeground},
    {"okHl", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(hl[OK]), XtRString, XtDefaultForeground},
    {"okIbd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(ibd[OK]), XtRString, XtDefaultForeground},
    {"okProg", "Program", XtRString, sizeof(char *),
	offset(prog[OK]), XtRString, NULL},
    {"retries", "Retries", XtRInt, sizeof(int),
	offset(retries), XtRString, "2"},
    {"rows", "Rows", XtRInt, sizeof(int),
	offset(rows), XtRString, "0"},
    {"scaleColl", "ScaleColl", XtRInt, sizeof(int),
	offset(scales[S_COLL]), XtRString, "1"},
    {"scaleCpu", "ScaleCpu", XtRInt, sizeof(int),
	offset(scales[S_CPU]), XtRString, "20"},
    {"scaleDisks", "ScaleDisks", XtRInt, sizeof(int),
	offset(scales[S_DISKS]), XtRString, "40"},
    {"scaleErr", "ScaleErr", XtRInt, sizeof(int),
	offset(scales[S_ERR]), XtRString, "1"},
    {"scaleIerr", "ScaleIerr", XtRInt, sizeof(int),
	offset(scales[S_IERR]), XtRString, "1"},
    {"scaleIntr", "ScaleIntr", XtRInt, sizeof(int),
	offset(scales[S_INTR]), XtRString, "50"},
    {"scaleIpkt", "ScaleIpkt", XtRInt, sizeof(int),
	offset(scales[S_IPKT]), XtRString, "20"},
    {"scaleLoad", "ScaleLoad", XtRInt, sizeof(int),
	offset(scales[S_LOAD]), XtRInt, (caddr_t) &loadscaledef},
    {"scaleOerr", "ScaleOerr", XtRInt, sizeof(int),
	offset(scales[S_OERR]), XtRString, "1"},
    {"scaleOpkt", "ScaleOpkt", XtRInt, sizeof(int),
	offset(scales[S_OPKT]), XtRString, "20"},
    {"scalePage", "ScalePage", XtRInt, sizeof(int),
	offset(scales[S_PAGE]), XtRString, "10"},
    {"scalePgpgin", "ScalePgpgin", XtRInt, sizeof(int),
	offset(scales[S_PGPGIN]), XtRString, "10"},
    {"scalePgpgout", "ScalePgpgout", XtRInt, sizeof(int),
	offset(scales[S_PGPGOUT]), XtRString, "10"},
    {"scalePkts", "ScalePkts", XtRInt, sizeof(int),
	offset(scales[S_PKTS]), XtRString, "40"},
    {"scalePswpin", "ScalePswpin", XtRInt, sizeof(int),
	offset(scales[S_PSWPIN]), XtRString, "5"},
    {"scalePswpout", "ScalePswpout", XtRInt, sizeof(int),
	offset(scales[S_PSWPOUT]), XtRString, "5"},
    {"scaleSwap", "ScaleSwap", XtRInt, sizeof(int),
	offset(scales[S_SWAP]), XtRString, "5"},
    {"scaleSwt", "ScaleSwt", XtRInt, sizeof(int),
	offset(scales[S_SWT]), XtRString, "30"},
    {"scaleSys", "ScaleSys", XtRInt, sizeof(int),
	offset(scales[S_SYS]), XtRString, "20"},
    {"scaleUser", "ScaleUser", XtRInt, sizeof(int),
	offset(scales[S_USER]), XtRString, "20"},
    {"shortName", "Shortname", XtRBoolean, sizeof(Boolean),
	offset(shortName), XtRString, "False"},
    {"timeout", "Timeout", XtRInt, sizeof(int),
	offset(timeout), XtRString, "5"},
    {"warnBack", XtCBackground, XtRPixel, sizeof(Pixel),
	offset(back[WARN]), XtRString, XtDefaultBackground},
    {"warnBd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(bd[WARN]), XtRString, XtDefaultForeground},
    {"warnBitmap", XtCBitmap, XtRBitmap, sizeof(Pixmap),
	offset(bitmap[WARN].bm), XtRBitmap, (caddr_t) &bitmapdef},
    {"warnFore", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(fore[WARN]), XtRString, XtDefaultForeground},
    {"warnHl", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(hl[WARN]), XtRString, XtDefaultForeground},
    {"warnIbd", XtCForeground, XtRPixel, sizeof(Pixel),
	offset(ibd[WARN]), XtRString, XtDefaultForeground},
    {"warnLevel", "WarnLevel", XtRInt, sizeof(int),
	offset(warnLevel), XtRString, "3"},
    {"warnProg", "Program", XtRString, sizeof(char *),
	offset(prog[WARN]), XtRString, NULL},
};
static XrmOptionDescRec	options[] = {
    {"-cols",		"columns",		XrmoptionSepArg, NULL},
    {"-dv",		"displayValue",		XrmoptionNoArg, "True"},
    {"-ebd",		"errorBd",		XrmoptionSepArg, NULL},
    {"-ebg",		"errorBack",		XrmoptionSepArg, NULL},
    {"-efg",		"errorFore",		XrmoptionSepArg, NULL},
    {"-ehl",		"errorHl",		XrmoptionSepArg, NULL},
    {"-eibd",		"errorIbd",		XrmoptionSepArg, NULL},
    {"-elevel",		"errorLevel",		XrmoptionSepArg, NULL},
    {"-ep",		"errorProg",		XrmoptionSepArg, NULL},
    {"-fbd",		"fatalBd",		XrmoptionSepArg, NULL},
    {"-fbg",		"fatalBack",		XrmoptionSepArg, NULL},
    {"-ffg",		"fatalFore",		XrmoptionSepArg, NULL},
    {"-fhl",		"fatalHl",		XrmoptionSepArg, NULL},
    {"-fibd",		"fatalIbd",		XrmoptionSepArg, NULL},
    {"-fp",		"fatalProg",		XrmoptionSepArg, NULL},
    {"-h",		"*Paned.height",	XrmoptionSepArg, NULL},
    {"-lebg",		"lErrorBack",		XrmoptionSepArg, NULL},
    {"-lefg",		"lErrorFore",		XrmoptionSepArg, NULL},
    {"-lfbg",		"lFatalBack",		XrmoptionSepArg, NULL},
    {"-lffg",		"lFatalFore",		XrmoptionSepArg, NULL},
    {"-lobg",		"lOkBack",		XrmoptionSepArg, NULL},
    {"-lofg",		"lOkFore",		XrmoptionSepArg, NULL},
    {"-lwbg",		"lWarnBack",		XrmoptionSepArg, NULL},
    {"-lwfg",		"lWarnFore",		XrmoptionSepArg, NULL},
    {"-obd",		"okBd",			XrmoptionSepArg, NULL},
    {"-obg",		"okBack",		XrmoptionSepArg, NULL},
    {"-ofg",		"okFore",		XrmoptionSepArg, NULL},
    {"-ohl",		"okHl",			XrmoptionSepArg, NULL},
    {"-oibd",		"okIbd",		XrmoptionSepArg, NULL},
    {"-op",		"okProg",		XrmoptionSepArg, NULL},
    {"-rows",		"rows",			XrmoptionSepArg, NULL},
    {"-rt",		"retries",		XrmoptionSepArg, NULL},
    {"-scoll",		"scaleColl",		XrmoptionSepArg, NULL},
    {"-scpu",		"scaleCpu",		XrmoptionSepArg, NULL},
    {"-sdisks",		"scaleDisks",		XrmoptionSepArg, NULL},
    {"-serr",		"scaleErr",		XrmoptionSepArg, NULL},
    {"-sierr",		"scaleIerr",		XrmoptionSepArg, NULL},
    {"-sintr",		"scaleIntr",		XrmoptionSepArg, NULL},
    {"-sipkt",		"scaleIpkt",		XrmoptionSepArg, NULL},
    {"-sload",		"scaleLoad",		XrmoptionSepArg, NULL},
    {"-sn",		"shortName",		XrmoptionNoArg, "True"},
    {"-soerr",		"scaleOerr",		XrmoptionSepArg, NULL},
    {"-sopkt",		"scaleOpkt",		XrmoptionSepArg, NULL},
    {"-spage",		"scalePage",		XrmoptionSepArg, NULL},
    {"-spgpgin",	"scalePgpgin",		XrmoptionSepArg, NULL},
    {"-spgpgout",	"scalePgpgout",		XrmoptionSepArg, NULL},
    {"-spkts",		"scalePkts",		XrmoptionSepArg, NULL},
    {"-spswpin",	"scalePswpin",		XrmoptionSepArg, NULL},
    {"-spswpout",	"scalePswpout",		XrmoptionSepArg, NULL},
    {"-sswap",		"scaleSwap",		XrmoptionSepArg, NULL},
    {"-sswt",		"scaleSwt",		XrmoptionSepArg, NULL},
    {"-ssys",		"scaleSys",		XrmoptionSepArg, NULL},
    {"-suser",		"scaleUser",		XrmoptionSepArg, NULL},
    {"-to",		"timeout",		XrmoptionSepArg, NULL},
    {"-update",		"*StripChart.update",	XrmoptionSepArg, NULL},
    {"-w",		"*Paned.width",		XrmoptionSepArg, NULL},
    {"-wbd",		"warnBd",		XrmoptionSepArg, NULL},
    {"-wbg",		"warnBack",		XrmoptionSepArg, NULL},
    {"-wfg",		"warnFore",		XrmoptionSepArg, NULL},
    {"-whl",		"warnHl",		XrmoptionSepArg, NULL},
    {"-wibd",		"warnIbd",		XrmoptionSepArg, NULL},
    {"-wlevel",		"warnLevel",		XrmoptionSepArg, NULL},
    {"-wp",		"warnProg",		XrmoptionSepArg, NULL},
};

static XtActionsRec	scact[] = {
    {"popupscale", popupscale},
    {"popdownscale", popdownscale}
};
#define NUMSCACT	(sizeof(scact) / sizeof(XtActionsRec))

static XtActionsRec	actions[] = {
    {"quit", quit},
};
Atom			wm_delete_window;

main(argc, argv)

int	argc;
char	**argv;

{
  Widget		toplevel;
  Widget		form;
  Widget		pane = NULL;
  int			i;
  int			n;
  Arg			args[10];
  XtAppContext		appcon;
  String		mbtrans =
      "<EnterWindow>:	highlight()\n\
       <LeaveWindow>:	reset()\n\
       <BtnDown>:	set() notify() PopupMenu()";
  XtTranslations	mbtt;
  String		sctrans =
      "<BtnDown>:	popupscale()\n\
       <BtnUp>:		popdownscale()";
  XtTranslations	scacttt;
  Widget		*lw;	/* Last column or row of widgets	*/
  int			numwgt;

  progname = argv[0];
  toplevel = XtAppInitialize(&appcon, "XMeter", options,
		XtNumber(options), &argc, argv, NULL, NULL, ZERO);
  if (argc < 2)
      usage();
  XtGetApplicationResources(toplevel, (XtPointer) &ar,
			    resources, XtNumber(resources), NULL, ZERO);
  form = XtCreateManagedWidget("form", formWidgetClass, toplevel, NULL, ZERO);
	/* Handle f.delete	*/
  XtAppAddActions(XtWidgetToApplicationContext(toplevel), actions,
		  XtNumber(actions));
  XtOverrideTranslations(toplevel,
	XtParseTranslationTable("<Message>WM_PROTOCOLS: quit()"));
  if (strcmp("-v", argv[1]) == 0)
       printversion();
  init(toplevel, &lw);
  createmenus(form);
  mbtt = XtParseTranslationTable(mbtrans);
  scacttt = XtParseTranslationTable(sctrans);
  XtAppAddActions(appcon, scact, NUMSCACT);
  /*
   * Each meter consists of a paned widget, each paned widget has two
   * children, which are a menubutton widget and stripchart widget.
   * The following loop creates and initializes the appropriate widgets.
   */
  for (i = 1, numwgt = 0; i < argc; i++, numwgt++) {
      meterlist = initmeter(meterlist, &i, argc, argv);
	/*
	 * Create paned widget.
	 */
      n = 0;
      if (ar.columns > 0) {		/* Locate pane in form ...	*/
	  XtSetArg(args[n], XtNfromVert, lw[numwgt % ar.columns]); n++;
	  if (numwgt % ar.columns != 0) {
	      XtSetArg(args[n], XtNfromHoriz, pane); n++;
	  }
      } else {
	  XtSetArg(args[n], XtNfromHoriz, lw[numwgt % ar.rows]); n++;
	  if (numwgt % ar.rows != 0) {
	      XtSetArg(args[n], XtNfromVert, pane); n++;
	  }
      }
      XtSetArg(args[n], XtNborder, ar.bd[OK]); n++;
      XtSetArg(args[n], XtNinternalBorderColor, ar.ibd[OK]); n++;
      pane = XtCreateManagedWidget(meterlist->sh->label, panedWidgetClass,
				   form, args, n);
      lw[numwgt % (ar.columns > 0 ? ar.columns : ar.rows)] = pane;
      meterlist->pdwidget = pane;
	/*
	 * Create menubutton widget.
	 */
      n = 0;
      XtSetArg(args[n], XtNshowGrip, XtEno); n++;
      XtSetArg(args[n], XtNlabel, meterlist->label); n++;
      XtSetArg(args[n], XtNbackground, ar.lback[OK]); n++;
      XtSetArg(args[n], XtNforeground, ar.lfore[OK]); n++;
      XtSetArg(args[n], XtNmenuName, STATMENU); n++;
      XtSetArg(args[n], XtNtranslations, mbtt); n++;
      meterlist->mbwidget = XtCreateManagedWidget("menu",
				menuButtonWidgetClass, pane, args, n);
      XtRemoveAllCallbacks(meterlist->mbwidget, XtNcallback);
      XtAddCallback(meterlist->mbwidget, XtNcallback,
		    (XtCallbackProc) selecthost, (XtPointer) meterlist);
	/*
	 * Create stripchart widget.
	 */
      n = 0;
      XtSetArg(args[n], XtNfromVert, meterlist->mbwidget); n++;
      XtSetArg(args[n], XtNresizable, TRUE); n++;
      XtSetArg(args[n], XtNbackground, ar.back[OK]); n++;
      XtSetArg(args[n], XtNforeground, ar.fore[OK]); n++;
      XtSetArg(args[n], XtNhighlight, ar.hl[OK]); n++;
      XtSetArg(args[n], XtNtranslations, scacttt); n++;
      meterlist->scwidget = XtCreateManagedWidget("load",
				stripChartWidgetClass, pane, args, n);
      XtRemoveAllCallbacks(meterlist->scwidget, XtNgetValue);
      XtAddCallback(meterlist->scwidget, XtNgetValue,
		    (XtCallbackProc) getstatus, (XtPointer) meterlist);
  }
  free(lw);
#ifdef SVR4
  sigset(SIGCHLD, freechild);
#else
  signal(SIGCHLD, freechild);		/* Clean up after our children	*/
#endif
  XtRealizeWidget(toplevel);
  wm_delete_window = XInternAtom(XtDisplay(toplevel), "WM_DELETE_WINDOW",
				 False);
  XSetWMProtocols(XtDisplay(toplevel), XtWindow(toplevel),
		  &wm_delete_window, 1);
  setokbackgrounds(XtDisplay(toplevel), XtScreen(toplevel));
  XtAppMainLoop(appcon);
  return(0);
}

usage()

{
  int	i;

  fprintf(stderr, "Usage: %s [toolkit options] [options] [stat] host ...\n",
	   progname);
  fprintf(stderr, "  Options are:");
  for (i = 0; i < XtNumber(options); i++) {
      if (i % 8 == 0)
          fprintf(stderr, "\n    ");
      else
	  fprintf(stderr, ", ");
      fprintf(stderr, "%s", options[i].option);
  }
  fprintf(stderr, ", -v\n  Stats to watch are:");
  for (i = 0; i < MAXSTAT; i++) {
      if (i % 8 == 0)
          fprintf(stderr, "\n    ");
      else
	  fprintf(stderr, ", ");
      fprintf(stderr, "-%s", sd[i].name);
  }
  fprintf(stderr, "\n");
  exit(1);
}

/*
 * printversion - Print version number and exit.
 */
printversion()

{
  printf("XMeter version %d.%d\n", MAJORVERSION, PATCHLEVEL);
  exit(0);
}

/*
 * init - Initialze various globals.
 */
init(toplevel, lw)

Widget	toplevel;
Widget	**lw;

{
  int		i;
  Window	rw;
  int		x;
  int		y;
  int		bwidth;
  int		depth;

  ptto.tv_sec = ar.timeout;
  tto.tv_sec = ar.timeout * ar.retries;
  if (ar.columns <= 0 && ar.rows <= 0)
      ar.columns = 1;
  if (ar.columns > 0) {
      if ((*lw = (Widget *) calloc(ar.columns, sizeof(Widget))) == NULL)
	  fatal("lw");	
  } else
      if ((*lw = (Widget *) calloc(ar.rows, sizeof(Widget))) == NULL)
	  fatal("lw");
  for (i = 0; i < MAXBACKS; i++)
      if (ar.bitmap[i].bm != bitmapdef)	/* Get bitmap dimensions	*/
	  XGetGeometry(XtDisplay(toplevel), ar.bitmap[i].bm, &rw, &x, &y,
		(unsigned int *) &ar.bitmap[i].w,
		(unsigned int *) &ar.bitmap[i].h,
		(unsigned int *) &bwidth, (unsigned int *) &depth);
}

/*
 * createmenus - Create menus used by each meter.  Currently there's just
 *   one which consists of the stats that can be monitored, and a quit
 *   command.
 */
createmenus(parent)

Widget	parent;

{
  int		i;
  int		n;
  Widget	menu;
  Widget	me;
  Arg		args[10];

  menu = XtCreatePopupShell(STATMENU, simpleMenuWidgetClass, parent,
			    NULL, ZERO);
  for (i = 0; i < MAXSTAT; i++) {
      n = 0;
      XtSetArg(args[n], XtNlabel, sd[i].name); n++;
      me = XtCreateManagedWidget(sd[i].name, smeBSBObjectClass, menu,
				 args, n);
      XtRemoveAllCallbacks(me, XtNcallback);
      XtAddCallback(me, XtNcallback,
		    (XtCallbackProc) changestat, (XtPointer) i);
  }
  n = 0;
  XtSetArg(args[n], XtNlabel, "quit"); n++;
  me = XtCreateManagedWidget("quit", smeBSBObjectClass, menu, args, n);
  XtRemoveAllCallbacks(me, XtNcallback);
  XtAddCallback(me, XtNcallback, (XtCallbackProc) quit, (XtPointer) NULL);
}

void
popupscale(w, event, params, num)

Widget		w;
XEvent		*event;
String		*params;
Cardinal	num;

{
  int		n;
  Arg		args[10];
  Position	x;
  Position	y;
  static char	buf[100];
  METER		*h;
  time_t	tdown;

  for (h = meterlist; h; h = h->nxt)
      if (h->scwidget == w)
	  break;
  if (!h) {
      fprintf(stderr, "popupscale: Bad stripchart widget\n");
      exit(1);
  }
  XtTranslateCoords(w, (Position) 0, (Position) 0, &x, &y);
  n = 0;
  XtSetArg(args[n], XtNx, x); n++;
  XtSetArg(args[n], XtNy, y); n++;
  popup = XtCreatePopupShell("popup", transientShellWidgetClass, w, args, n);
  if (h->sh->pid != -1) {
      tdown = time(0) - h->sh->when;
      sprintf(buf, "%s\nversion %d.%d\nscale %d\ndown %02d:%02d:%02d",
	      h->sh->name,
	      MAJORVERSION, PATCHLEVEL,
	      h->stat == S_LOAD ? ar.scales[h->stat] / FSCALE :
				  ar.scales[h->stat],
	      tdown / (60*60), (tdown / 60) % 60, tdown % 60);
  } else
      sprintf(buf, "%s\nversion %d.%d\nscale %d",
	      h->sh->name,
	      MAJORVERSION, PATCHLEVEL,
	      h->stat == S_LOAD ? ar.scales[h->stat] / FSCALE :
				  ar.scales[h->stat]);
  n = 0;
  XtSetArg(args[n], XtNlabel, buf); n++;
  XtCreateManagedWidget("label", labelWidgetClass, popup, args, n);
  XtPopup(popup, XtGrabNone);
}

void
popdownscale(w, event, params, num)

Widget		w;
XEvent		*event;
String		*params;
Cardinal	num;

{
  if (popup)			/* Can be NULL in some cases apparently	*/
      XtDestroyWidget(popup);
}

/*
 * setokbackgrounds - Set initial background pixmaps.  Can't do this
 *   when the stripchart widgets are created as they have to be
 *   realized before the pixmaps can be created.
 */
setokbackgrounds(d, s)

Display	*d;
Screen	*s;

{
  METER		*h;
  int		n;
  Arg		args[10];

  if (ar.bitmap[OK].bm != bitmapdef) {
      for (h = meterlist; h; h = h->nxt) {
	  h->pm = XmuCreatePixmapFromBitmap(d, XtWindow(h->scwidget),
					    ar.bitmap[OK].bm,
					    ar.bitmap[OK].w, ar.bitmap[OK].h,
					    DefaultDepthOfScreen(s),
					    ar.fore[OK],
					    ar.back[OK]);
	  n = 0;
	  XtSetArg(args[n], XtNbackgroundPixmap, h->pm); n++;
	  XtSetValues(h->scwidget, args, n);
      }
  }
}

/*
 * freechild - Clean up child processes.
 */
SIGTYPE
freechild(sig)

int	sig;

{
  int	pid;
  METER	*h;

  pid = wait(NULL);
#ifdef SYSV
#ifdef SVR4
  sigset(SIGCHLD, freechild);
#else !SVR4
  signal(SIGCHLD, freechild);
#endif SVR4
#endif SYSV
  if (pid <= 0)
      return;
  for (h = meterlist; h; h = h->nxt)
      if (h->sh->pid == pid) {		/* Start updating host again	*/
	  h->sh->pid = -1;
	  break;
      }
}

/*
 * getstatus - Get status from remote host, update meter appropriately,
 *   including changing colors, background pixmap and label if necessary.
 */
getstatus(w, h, data)

Widget	w;
METER	*h;
char	*data;

{
  int			l;
  int			n;
  int			s;
  register SHMETER	*sh;
  Arg			args[10];

  sh = h->sh;
  if (h->oldstate == FATAL && sh->pid != -1) {	/* Ignore dead hosts	*/
      *(double *) data = 0.0;
      return;
  }
  if (h->oldjumpscroll) {		/* Restore old jumpscroll value	*/
      n = 0;
      XtSetArg(args[n], XtNjumpScroll, h->oldjumpscroll); n++;
      XtSetValues(h->scwidget, args, n);
      h->oldjumpscroll = 0;
  }
  s = state(l = getmeter(h), h);
  if (ar.displayValue) {
      n = 0;				/* Update menubutton		*/
      XtSetArg(args[n], XtNbackground, ar.lback[s]); n++;
      XtSetArg(args[n], XtNforeground, ar.lfore[s]); n++;
      sprintf(h->label, "%s %s %.2f", sh->label,
	      s == FATAL ? DMSG : sd[h->stat].name,
	      s == FATAL ? 0.0 : (float) l / ar.scales[h->stat]);
      XtSetArg(args[n], XtNlabel, h->label); n++;
      XtSetValues(h->mbwidget, args, n);
  }
  *(double *) data = (s == FATAL) ? 0.0 : (double) l / ar.scales[h->stat];
  if (s != h->oldstate) {
      sh->when = time(0);
      if (ar.bitmap[h->oldstate].bm != bitmapdef)
	  XFreePixmap(XtDisplay(w), h->pm);
      n = 0;				/* Update stripchart widget	*/
      XtSetArg(args[n], XtNbackground, ar.back[s]); n++;
      XtSetArg(args[n], XtNforeground, ar.fore[s]); n++;
      XtSetArg(args[n], XtNhighlight, ar.hl[s]); n++;
      if (ar.bitmap[s].bm != bitmapdef) {
	  h->pm = XmuCreatePixmapFromBitmap(XtDisplay(w),
					    XtWindow(w),
					    ar.bitmap[s].bm,
					    ar.bitmap[s].w, ar.bitmap[s].h,
					    DefaultDepthOfScreen(XtScreen(w)),
					    ar.fore[s],
					    ar.back[s]);
	  XtSetArg(args[n], XtNbackgroundPixmap, h->pm); n++;
      } else if (ar.bitmap[h->oldstate].bm != bitmapdef) {
	  XtSetArg(args[n], XtNbackgroundPixmap, XtUnspecifiedPixmap); n++;
      }
      XtSetValues(w, args, n);
      n = 0;				/* Update menubutton widget	*/
      XtSetArg(args[n], XtNbackground, ar.lback[s]); n++;
      XtSetArg(args[n], XtNforeground, ar.lfore[s]); n++;
      if (ar.displayValue) {
	  sprintf(h->label, "%s %s %.2f", sh->label,
		  s == FATAL ? DMSG : sd[h->stat].name,
		  s == FATAL ? 0.0 : (float) l / ar.scales[h->stat]);
	  XtSetArg(args[n], XtNlabel, h->label); n++;
      } else if (s == FATAL || h->oldstate == FATAL) {
	  sprintf(h->label, "%s %s", sh->label,
		   s == FATAL ? DMSG : sd[h->stat].name);
	  XtSetArg(args[n], XtNlabel, h->label); n++;
      }
      XtSetValues(h->mbwidget, args, n);
      n = 0;				/* Update paned widget		*/
      XtSetArg(args[n], XtNborder, ar.bd[s]); n++;
      XtSetArg(args[n], XtNinternalBorderColor, ar.ibd[s]); n++;
      XtSetValues(h->pdwidget, args, n);
      if (ar.prog[s])
	  runprog(h, s);
      if (s == FATAL)
	  sh->pid = waitforhost(h);
  }
  h->oldstate = s;
}

/*
 * waitforhost - Fork process which will wait for host to come back up.
 *   Child exits if parent is init (pid 1), to handle case where parent
 *   has gone away.
 */
int
waitforhost(h)

METER	*h;

{
  int	pid;

  if (pid = fork())
      return(pid);
#ifdef SVR4
  sigset(SIGTERM, SIG_DFL);
#else !SVR4
  signal(SIGTERM, SIG_DFL);
#endif !SVR4
  while (1) {
      sleep(10);
      if (getport(h) > 0 || getppid() < 2)
	  exit(0);
  }
}

/*
 * runprog - Run user specified alert program.
 */
runprog(h, s)

METER	*h;
int	s;

{
  char	oldstate[4];
  char	state[4];

  sprintf(oldstate, "%d", h->oldstate);
  sprintf(state, "%d", s);
  if (vfork())
      return;			/* Parent just returns		*/
  execlp(ar.prog[s], ar.prog[s], oldstate, state,
	 h->sh->label, sd[h->stat].name, 0);
  fatal(ar.prog[s]);
}

/*
 * selecthost - Select host for next changestat().
 */
selecthost(w, h, data)

Widget	w;
METER	*h;
char	*data;

{
  selected = h;
}

/*
 * changestat - Change statistic we're looking at.  To clear the stripchart
 *   it's necessary to poke the "interval" field in the StripChartWidget
 *   structure, and then set "jumpScroll" to the width of the stripchart.
 *   jumpScroll is saved here and restored in getstatus().
 */
#include <X11/IntrinsicP.h>
#include <X11/Xaw/StripCharP.h>
changestat(w, statidx, data)

Widget	w;
int	statidx;
char	*data;

{
  int	n;
  Arg	args[10];
  int	width;

  selected->stat = statidx;
  sprintf(selected->label, "%s %s", selected->sh->label, sd[statidx].name);
  n = 0;
  XtSetArg(args[n], XtNlabel, selected->label); n++;
  XtSetValues(selected->mbwidget, args, n);
  n = 0;
  XtSetArg(args[n], XtNwidth, &width); n++;
  XtSetArg(args[n], XtNjumpScroll, &selected->oldjumpscroll); n++;
  XtGetValues(selected->scwidget, args, n);
  n = 0;
  XtSetArg(args[n], XtNjumpScroll, width); n++;
  XtSetValues(selected->scwidget, args, n);
  ((StripChartWidget) selected->scwidget)->strip_chart.interval = width;
}

/*
 * state - Return state of current stat.
 */
int
state(l, h)

int	l;
METER	*h;

{
  if (l < 0)
      return(FATAL);
  else if (l < ar.warnLevel * ar.scales[h->stat])
      return(OK);
  else if (l < ar.errorLevel * ar.scales[h->stat])
      return(WARN);
  else
      return(ERROR);
}

/*
 * The following functions return the value of the specified stat, which
 * is normally computed by taking the difference between the current
 * value and the previous value, and dividing by the update interval in
 * order to get the current rate.
 */
#define DIF(m,fld)	(m->sh->st[m->sh->idx].fld - \
			 m->sh->st[m->sh->idx ^ 1].fld)
#define INT(m)		(DIF(m,curtime.tv_sec) <= 0 ? 1 : \
						      DIF(m,curtime.tv_sec))

int
fcoll(h)

METER	*h;

{
  return(DIF (h, if_collisions) / INT(h));
}

int
fcpu(h)

METER	*h;

{
  int	i;
  int	t;
  int	d[CPUSTATES];

  for (t = 0, i= 0; i < CPUSTATES; i++)
      t += (d[i] = DIF (h, cp_time[i]));
  return(t ? (100 * (d[CP_USER]+d[CP_NICE]+d[CP_SYS])) / t : 0);
}

int
fdisks(h)

METER	*h;

{
  int	i;
  int	t;

  for (t = 0, i= 0; i < 4; i++)
      t += (DIF (h, dk_xfer[i]));
  return(t / INT(h));
}

int
ferr(h)

METER	*h;

{
  return(fierr(h) + foerr(h));
}

int
fierr(h)

METER	*h;

{
  return(DIF (h, if_ierrors) / INT(h));
}

int
fintr(h)

METER	*h;

{
  return(DIF (h, v_intr) / INT(h));
}

int
fipkt(h)

METER	*h;

{
  return(DIF (h, if_ipackets) / INT(h));
}

int
fload(h)

METER	*h;

{
  return(h->sh->st[h->sh->idx].avenrun[0]);
}

int
foerr(h)

METER	*h;

{
  return(DIF (h, if_oerrors) / INT(h));
}

int
fopkt(h)

METER	*h;

{
  return(DIF (h, if_opackets) / INT(h));
}

int
fpage(h)

METER	*h;

{
  return((DIF (h, v_pgpgin) + DIF (h, v_pgpgout)) / INT(h));
}

int
fpgpgin(h)

METER	*h;

{
  return(DIF (h, v_pgpgin) / INT(h));
}

int
fpkts(h)

METER	*h;

{
  return(fipkt(h) + fopkt(h));
}

int
fpgpgout(h)

METER	*h;

{
  return(DIF (h, v_pgpgout) / INT(h));
}

int
 fswap(h)

METER	*h;

{
  return(fpswpin(h) + fpswpout(h));
}

int
 fpswpin(h)

METER	*h;

{
  return(DIF (h, v_pswpin) / INT(h));
}

int
fpswpout(h)

METER	*h;

{
  return(DIF (h, v_pswpout) / INT(h));
}

int
fswt(h)

METER	*h;

{
  return(DIF (h, v_swtch) / INT(h));
}

int
fsys(h)

METER	*h;

{
  int	i;
  int	t;
  int	d[CPUSTATES];

  for (t = 0, i= 0; i < CPUSTATES; i++)
      t += (d[i] = DIF (h, cp_time[i]));
  return(t ? (100 * d[CP_SYS]) / t : 0);
}

int
fuser(h)

METER	*h;

{
  int	i;
  int	t;
  int	d[CPUSTATES];

  for (t = 0, i= 0; i < CPUSTATES; i++)
      t += (d[i] = DIF (h, cp_time[i]));
  return(t ? (100 * (d[CP_USER] + d[CP_NICE])) / t : 0);
}

/*
 * getmeter - Executes rstat(3) call to read statistics for specified host.
 *   I do all the rpc junk myself so that I have better control over timeouts
 *   than rstat(3) gives me.  If we're watching multiple stats
 *   on the same host I only do one rstat(3) call (refcnt and curcnt are
 *   used for this).
 */
int
getmeter(h)

register METER	*h;

{
  enum clnt_stat	cs;
  register SHMETER	*sh;
  int			p;

  sh = h->sh;
  if (sh->curcnt >= sh->refcnt)
      sh->curcnt = 0;
  if (!sh->curcnt++) {
      if (sh->clnt == NULL) {
          if ((p = getport(h)) <= 0)
	      return(-1);
	  sh->addr.sin_port = htons(p);
          sh->s = RPC_ANYSOCK;
          if (!(sh->clnt = clntudp_create(&sh->addr, RSTATPROG, RSTATVERS_TIME,
					  ptto, &sh->s)))
	      return(-1);
	  sh->first = 1;
          sh->idx = 0;
      } else {
	  sh->first = 0;
	  sh->idx ^= 1;
      }
      cs = clnt_call(sh->clnt, RSTATPROC_STATS, xdr_void, 0, xdr_statstime,
		     (caddr_t) &sh->st[sh->idx], tto);
      if (cs != RPC_SUCCESS) {
          clnt_destroy(sh->clnt);
	  close(sh->s);	/* Some clnt_destroy's don't do this	*/
          sh->clnt = NULL;
          return(-1);
      }
  }
  return(sh->first ? 0 : sh->clnt == NULL ? -1 : (sd[h->stat].val)(h));
}

/*
 * getport - Get port rstatd is listening on.
 */
int
getport(h)

METER	*h;

{
  CLIENT		*c;
  enum clnt_stat	cs;
  static struct pmap	pm = {RSTATPROG, RSTATVERS_TIME, IPPROTO_UDP, 0};
  unsigned short	p;
  register SHMETER	*sh;

  sh = h->sh;
  sh->s = RPC_ANYSOCK;
  sh->addr.sin_port = htons(PMAPPORT);
  if (!(c = clntudp_create(&sh->addr, PMAPPROG, PMAPVERS, ptto, &sh->s)))
      return(-1);
  cs = clnt_call(c, PMAPPROC_GETPORT, xdr_pmap, (caddr_t) &pm,
		 xdr_u_short, (caddr_t) &p, tto);
  clnt_destroy(c);
  close(sh->s);		/* Some clnt_destroys don't do this	*/
  return(cs == RPC_SUCCESS ? p : -1);
}

/*
 * initmeter - Fill in METER and SHMETER structures for this stat.
 */
METER
*initmeter(meterlist, idx, argc, argv)

METER	*meterlist;
int	*idx;
int	argc;
char	**argv;

{
  register METER	*h;
  struct hostent	*he;
  int			i;
  char			*cp;

  /*
   * Create and fill in METER struct.
   */
  if (!(h = (METER *) malloc(sizeof(METER))))
      fatal("METER");
  h->nxt = meterlist;
  cp = (argv[*idx][0] == '-') ? &argv[*idx][1] : ar.defStat;
  for (i = 0; i < MAXSTAT; i++)
      if (strcmp(cp, sd[i].name) == 0) {
	  h->stat = i;
	  break;
      }
  if ((cp != ar.defStat) && (++(*idx) == argc || i >= MAXSTAT))
      usage();
  if ((he = gethostbyname(argv[*idx])) == NULL)
       fatal(argv[*idx]);
  h->oldstate = OK;
  h->oldjumpscroll = 0;
  h->sh = newshmeter(he);
  if (!(h->label = (char *) malloc(strlen(h->sh->label) + 2 + MAXSTATNAME +
				   MAXSTATVALUE + 6)))
      fatal("label");
  sprintf(h->label, "%s %s", h->sh->label, sd[h->stat].name);
  return(h);
}

/*
 * newshmeter - Return SHMETER structure for host.  Keeps
 *   reference count for each SHMETER.  If an SHMETER for this host
 *   doesn't already exist then one is created and saved on the shmeter
 *   linked list.  Note that he->h_name gets stepped on by this routine
 *   after it is saved in sh->name.
 *   
 */
SHMETER
*newshmeter(he)

struct hostent	*he;

{
  SHMETER	*sh;
  char		*cp;

  /*
   * If we're already looking at this host then just return existing
   * structure.
   */
  for (sh = shmeters; sh; sh = sh->nxt)
      if (strcmp(sh->name, he->h_name) == 0) {
	  sh->refcnt++;		/* Count how many references		*/
	  return(sh);
      }
  /*
   * Not looking at this host yet, create and fill in SHMETER struct.
   */
  if (!(sh = (SHMETER *) malloc(sizeof(SHMETER))))
      fatal("SHMETER");
  sh->nxt = shmeters;		/* Save this struct in linked list	*/
  shmeters = sh;
  if (!(sh->name = mystrdup(he->h_name)))
      fatal("newshmeter");
  if (ar.shortName && (cp = strchr(he->h_name, '.'))) {
      *cp = '\0';
      if (!(sh->label = mystrdup(he->h_name)))
	  fatal("initmeter");
  } else
      sh->label = sh->name;
  sh->addr.sin_family = AF_INET;
  sh->addr.sin_addr.s_addr = *(int *) he->h_addr;
  sh->clnt = NULL;
  sh->refcnt = 1;
  sh->curcnt = 0;
  sh->pid = -1;
  return(sh);
}

/*
 * mystrdup - Not everyone has strdup() in their C library, so provide
 *   our own.
 */
char *
mystrdup(s)

char	*s;

{
  char	*t;

  if (t = (char *) malloc(strlen(s) + 1))
      return(strcpy(t, s));
  else
      return(NULL);
}

void
quit(w)

Widget	w;

{
  XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  exit(0);
}

fatal(m)

char	*m;

{
  perror(m);
  exit(1);
}