home *** CD-ROM | disk | FTP | other *** search
- /***********************************************************
- Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
-
- All Rights Reserved
-
- Permission to use, copy, modify, and distribute these examples for any
- purpose and without fee is hereby granted, provided that the above
- copyright notice appear in all copies and that both that copyright
- notice and this permission notice appear in supporting documentation,
- and that the name of Digital not be used in advertising or publicity
- pertaining to distribution of the software without specific, written
- prior permission.
-
- DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
- SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
- FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
- OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
- OR PERFORMANCE OF THIS SOFTWARE.
-
- ******************************************************************/
-
- #include <stdio.h>
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include "Graph.h"
- #include "BarDisplay.h"
-
- XtAppContext app;
- Widget graph;
- int *fields;
-
- void Timer(), CreateGraphWidget(), GetGraphData();
- typedef struct {
- int timeout;
- int num_fields;
- String command;
- } OptionsRec;
-
- OptionsRec options;
-
- #define Offset(field) XtOffsetOf(OptionsRec, field)
-
- XtResource resources[] = {
- {"interval", "Interval", XtRInt, sizeof(int),
- Offset(timeout), XtRImmediate, (XtPointer) 5},
- {"numFields", "NumFields", XtRInt, sizeof(int),
- Offset(num_fields), XtRImmediate, (XtPointer) 3},
- {"command", "Command", XtRString, sizeof(String),
- Offset(command), XtRString,
- "vmstat 1 2 | awk '{if (NR == 4) print $(NF-2), $(NF-1), $(NF)}'"}
- };
-
- #undef Offset
-
- XrmOptionDescRec optionDesc[] = {
- {"-interval", "*interval", XrmoptionSepArg, (XtPointer) NULL}
- };
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- Widget toplevel;
-
- toplevel = XtAppInitialize(&app, "DemoMonitor",
- optionDesc, XtNumber(optionDesc), &argc, argv,
- (String *) NULL, (ArgList) NULL, 0);
-
- XtGetApplicationResources(toplevel, (XtPointer) &options,
- resources, XtNumber(resources), (Arg *) NULL, 0);
-
- fields = (int *) XtCalloc(options.num_fields, sizeof(int));
-
- CreateGraphWidget(toplevel);
- XtRealizeWidget(toplevel);
-
- (void) XtAppAddTimeOut(app, options.timeout * 1000,
- Timer, (XtPointer) NULL);
-
- XtAppMainLoop(app);
- }
-
- void CreateGraphWidget(parent)
- Widget parent;
- {
- Arg args[10];
- int n;
-
- GetGraphData();
-
- n = 0;
- XtSetArg(args[n], XtNnumEntries, options.num_fields); n++;
- XtSetArg(args[n], XtNvalues, fields); n++;
- graph = XtCreateManagedWidget("graph", graphWidgetClass, parent,
- args, n);
-
- (void) XtCreateWidget("bar", barDisplayObjectClass, graph,
- (Arg *) NULL, 0);
- }
-
- void Timer(client_data, id)
- XtPointer client_data;
- XtIntervalId *id;
- {
- Arg args[10];
- int n;
-
- GetGraphData();
-
- n = 0;
- XtSetArg(args[n], XtNvalues, fields); n++;
- XtSetValues(graph, args, n);
-
- (void) XtAppAddTimeOut(app, options.timeout * 1000,
- Timer, (XtPointer) NULL);
- }
-
- void GetGraphData()
- {
- int status;
- FILE *f;
- int i;
-
- f = popen(options.command, "r");
-
- for (i = 0; i < options.num_fields; i++) {
- status = fscanf(f, "%d", &fields[i]);
- if (status != 1) {
- XtAppWarningMsg(app, "noData", "getGraphData",
- "DemoLoadError", "Not enough fields in data",
- (String *) NULL, (Cardinal *) NULL);
- for (; i < options.num_fields; i++) fields[i] = 0;
- break;
- }
- }
-
- pclose(f);
- }
-