"Fossies" - the Fresh Open Source Software Archive

Member "xload-snmp-0.5/xload.c" (17 Mar 2003, 13103 Bytes) of package /linux/privat/old/xload-snmp-0.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "xload.c" see the Fossies "Dox" file reference documentation.

    1 /** 
    2  ** xload-snmp <alh@warhound.org>
    3  ** Changes to this software differing from the X Consortium release
    4  ** versions below are Copyright (c) 2000 Adrian Hosey. Changes are
    5  ** licensed under the X Consortium license as described below.
    6  **/
    7 
    8 /* $XConsortium: xload.c,v 1.37 94/04/17 20:43:44 converse Exp $ */
    9 /* $XFree86: contrib/programs/xload/xload.c,v 3.0 1994/06/05 08:00:09 dawes Exp $ */
   10 /*
   11 
   12 Copyright (c) 1989  X Consortium
   13 
   14 Permission is hereby granted, free of charge, to any person obtaining
   15 a copy of this software and associated documentation files (the
   16 "Software"), to deal in the Software without restriction, including
   17 without limitation the rights to use, copy, modify, merge, publish,
   18 distribute, sublicense, and/or sell copies of the Software, and to
   19 permit persons to whom the Software is furnished to do so, subject to
   20 the following conditions:
   21 
   22 The above copyright notice and this permission notice shall be included
   23 in all copies or substantial portions of the Software.
   24 
   25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   26 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   28 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
   29 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   30 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   31 OTHER DEALINGS IN THE SOFTWARE.
   32 
   33 Except as contained in this notice, the name of the X Consortium shall
   34 not be used in advertising or otherwise to promote the sale, use or
   35 other dealings in this Software without prior written authorization
   36 from the X Consortium.
   37 
   38 */
   39 
   40 /*
   41  * xload-snmp - display system load average in a window with SNMP ability
   42  */
   43 
   44 #include <stdio.h> 
   45 #include <X11/Intrinsic.h>
   46 #include <X11/Xatom.h>
   47 #include <X11/StringDefs.h>
   48 #include <X11/Shell.h>
   49 
   50 #include <X11/Xaw/Cardinals.h>
   51 #include <X11/Xaw/Label.h>
   52 #include <X11/Xaw/Paned.h>
   53 #include <X11/Xaw/StripChart.h>
   54 #include <X11/Xmu/SysUtil.h>
   55 
   56 #include "xload-snmp.h"
   57 #include "xload.bit"
   58 
   59 char *ProgramName;
   60 
   61 extern void exit(), GetLoadPoint(), GetLoadPoint_SNMP();
   62 static void quit();
   63 static void ClearLights();
   64 static void SetLights();
   65 
   66 /*
   67  * Definition of the Application resources structure.
   68  */
   69 
   70 typedef struct _XLoadSNMPResources {
   71   Boolean show_label;
   72   Boolean use_lights;
   73   String peername;
   74   String community;
   75   String oid;
   76   float factor;
   77   Boolean delta;
   78 } XLoadSNMPResources;
   79 
   80 /*
   81  * Command line options table.  Only resources are entered here...there is a
   82  * pass over the remaining options after XtParseCommand is let loose. 
   83  */
   84 
   85 static XrmOptionDescRec options[] = {
   86  {"-scale",     "*load.minScale",   XrmoptionSepArg,    NULL},
   87  {"-update",        "*load.update",     XrmoptionSepArg,    NULL},
   88  {"-hl",        "*load.highlight",  XrmoptionSepArg,    NULL},
   89  {"-highlight",     "*load.highlight",  XrmoptionSepArg,    NULL},
   90  {"-label",     "*label.label",     XrmoptionSepArg,    NULL},
   91  {"-nolabel",       "*showLabel",           XrmoptionNoArg,       "False"},
   92  {"-lights",        "*useLights",       XrmoptionNoArg,       "True"},
   93  {"-jumpscroll",    "*load.jumpScroll", XrmoptionSepArg,    NULL},
   94  {"-factor",            "*factor",              XrmoptionSepArg,        NULL},
   95  {"-peername",          "*peername",            XrmoptionSepArg,        NULL},
   96  {"-community",         "*community",           XrmoptionSepArg,        NULL},
   97  {"-oid",               "*oid",                 XrmoptionSepArg,        NULL},
   98  {"-delta",             "*delta",               XrmoptionNoArg,       "True"},
   99 };
  100 
  101 /*
  102  * The structure containing the resource information for the
  103  * XloadSNMP application resources.
  104  */
  105 
  106 #define Offset(field) (XtOffsetOf(XLoadSNMPResources, field))
  107 
  108 /* I can't believe I have to do this. Xt is an atrocity. */
  109 static float factorDefault = 1.0;
  110 static XtResource my_resources[] = {
  111   {"showLabel", XtCBoolean, XtRBoolean, sizeof(Boolean),
  112      Offset(show_label), XtRImmediate, (XtPointer) TRUE},
  113   {"useLights", XtCBoolean, XtRBoolean, sizeof(Boolean),
  114     Offset(use_lights), XtRImmediate, (XtPointer) FALSE},
  115   {"factor", XtCValue, XtRFloat, sizeof(float),
  116    Offset(factor), XtRFloat, (XtPointer) &factorDefault },
  117   {"peername", XtCString, XtRString, sizeof(String),
  118    Offset(peername), XtRString, (XtPointer)NULL },
  119   {"community", XtCString, XtRString, sizeof(String),
  120    Offset(community), XtRString, (XtPointer)NULL },
  121   {"oid", XtCString, XtRString, sizeof(String),
  122    Offset(oid), XtRString, (XtPointer)NULL },
  123   {"delta", XtCBoolean, XtRBoolean, sizeof(Boolean),
  124    Offset(delta), XtRImmediate, (XtPointer) FALSE },
  125 };
  126 
  127 #undef Offset
  128 
  129 static XLoadSNMPResources resources;
  130 
  131 static XtActionsRec xloadsnmp_actions[] = {
  132     { "quit",   quit },
  133 };
  134 static Atom wm_delete_window;
  135 static int light_update = 10 * 1000;
  136 
  137 /*
  138  * Exit with message describing command line format.
  139  */
  140 
  141 void usage()
  142 {
  143     fprintf (stderr, "usage:  %s [-options ...]\n\n", ProgramName);
  144     fprintf (stderr, "where options include:\n");
  145     fprintf (stderr,
  146       "    -display dpy            X server on which to display\n");
  147     fprintf (stderr,
  148       "    -geometry geom          size and location of window\n");
  149     fprintf (stderr, 
  150       "    -fn font                font to use in label\n");
  151     fprintf (stderr, 
  152       "    -scale number           minimum number of scale lines\n");
  153     fprintf (stderr, 
  154       "    -update seconds         interval between updates\n");
  155     fprintf (stderr,
  156       "    -label string           annotation text\n");
  157     fprintf (stderr, 
  158       "    -bg color               background color\n");
  159     fprintf (stderr, 
  160       "    -fg color               graph color\n");
  161     fprintf (stderr, 
  162       "    -hl color               scale and text color\n");
  163     fprintf (stderr, 
  164       "    -nolabel                removes the label from above the chart.\n");
  165     fprintf (stderr, 
  166       "    -jumpscroll value       number of pixels to scroll on overflow\n");
  167     fprintf (stderr,
  168       "    -factor value           floating point value to \"scale down\" the plotted values\n");
  169     fprintf (stderr,
  170       "    -peername string        name of a host for SNMP querying\n");
  171     fprintf (stderr,
  172       "    -community string       name of file containing the community string\n");
  173     fprintf (stderr,
  174       "    -oid string             an oid for doing SNMP querying\n");
  175     fprintf (stderr,
  176       "    -delta                  plot the difference between values (snmp only)\n");
  177     fprintf (stderr, "\n");
  178     exit(1);
  179 }
  180 
  181 void main(argc, argv)
  182     int argc;
  183     char **argv;
  184 {
  185     XtAppContext app_con;
  186     Widget toplevel, load, pane, label_wid, load_parent;
  187     Arg args[1];
  188     Pixmap icon_pixmap = None;
  189     char *label, host[256];
  190     int want_snmp = 0;
  191     snmp_params my_snmp;
  192 
  193     ProgramName = argv[0];
  194 
  195     /* For security reasons, we reset our uid/gid after doing the necessary
  196        system initialization and before calling any X routines. */
  197     InitLoadPoint();
  198     setgid(getgid());       /* reset gid first while still (maybe) root */
  199     setuid(getuid());
  200 
  201     toplevel = XtAppInitialize(&app_con, "XLoadSNMP", options, 
  202                    XtNumber(options),
  203                    &argc, argv, NULL, NULL, (Cardinal) 0);
  204     if (argc != 1) usage();
  205 
  206     XtGetApplicationResources( toplevel, (XtPointer) &resources, 
  207                   my_resources, XtNumber(my_resources),
  208                   NULL, (Cardinal) 0);
  209     
  210     if (resources.use_lights)
  211     {
  212     char        name[1024];
  213     XrmString   type;
  214     XrmValue    db_value;
  215     XrmValue    int_value;
  216     Bool        found = False;
  217 
  218     (void) sprintf (name, "%s.paned.load.update", XtName(toplevel));
  219     found = XrmGetResource (XtScreenDatabase(XtScreen(toplevel)),
  220                 name, "XLoadSNMP.Paned.StripChart.Interval",
  221                 &type, &db_value);
  222     if (found) {
  223         int_value.size = sizeof(int);
  224         int_value.addr = (XPointer) &light_update;
  225         found = XtConvertAndStore(toplevel, type, &db_value, XtRInt,
  226                       &int_value);
  227         if (found) light_update *= 1000;
  228     }
  229     ClearLights (XtDisplay (toplevel));
  230     SetLights ((XtPointer) toplevel, (XtIntervalId *) 0);
  231     }
  232     else
  233     {
  234         /*
  235          * This is a hack so that f.delete will do something useful in this
  236          * single-window application.
  237          */
  238         XtAppAddActions (app_con, xloadsnmp_actions, 
  239              XtNumber(xloadsnmp_actions));
  240         XtOverrideTranslations(toplevel,
  241                 XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
  242     
  243         XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
  244         XtGetValues(toplevel, args, ONE);
  245         if (icon_pixmap == None) {
  246         XtSetArg(args[0], XtNiconPixmap, 
  247              XCreateBitmapFromData(XtDisplay(toplevel),
  248                            XtScreen(toplevel)->root,
  249                            (char *)xload_bits,
  250                            xload_width, xload_height));
  251         XtSetValues (toplevel, args, ONE);
  252         }
  253     
  254         if (resources.show_label) {
  255           pane = XtCreateManagedWidget ("paned", panedWidgetClass,
  256                         toplevel, NULL, ZERO);
  257     
  258           label_wid = XtCreateManagedWidget ("label", labelWidgetClass,
  259                          pane, NULL, ZERO);
  260           
  261           XtSetArg (args[0], XtNlabel, &label);
  262           XtGetValues(label_wid, args, ONE);
  263           
  264           if ( strcmp("label", label) == 0 ) {
  265 #ifdef AMOEBA
  266               char *s;
  267               char *getenv();
  268        
  269               host[255] = '\0';
  270               if ((s = getenv("XLOAD_HOST")) != NULL) {
  271                    strncpy(host, s, 255);
  272               } else if ((s = getenv("RUN_SERVER")) != NULL) {
  273                   /* specific runserver specified; use its name */
  274                   strncpy(host, s, 255);
  275        
  276                   {   /* if the last component is ".run", remove it */
  277                       char *slash = strrchr(host, '/');
  278                       if (slash != NULL && strcmp(slash + 1, ".run") == 0) {
  279                           *slash = '\0';
  280                       }
  281                   }
  282               } else {
  283                   (void) XmuGetHostname (host, 255);  /* "amoeba" */
  284               }
  285 #else
  286         (void) XmuGetHostname (host, 255);
  287 #endif
  288         XtSetArg (args[0], XtNlabel, host);
  289         XtSetValues (label_wid, args, ONE);
  290           }
  291     
  292           load_parent = pane;
  293         }
  294         else
  295           load_parent = toplevel;
  296     
  297         load = XtCreateManagedWidget ("load", stripChartWidgetClass,
  298                       load_parent, NULL, ZERO);    
  299 
  300     /* Check if any of the SNMP flags are defined. If so,
  301      * initialize for SNMP and setup the appropriate callback. 
  302      */
  303     if (resources.peername || resources.community || resources.oid) {
  304         want_snmp = 1;
  305         /* Make sure all three are defined. */
  306         if (! (resources.peername && resources.oid && 
  307            resources.community)) {
  308         fprintf(stderr, "When doing SNMP, all of -peername, -oid, "
  309             "and -community are required.\nDo 'man xload' for "
  310             "help\n");
  311         exit(1);
  312         }
  313         /* A factor of 0.0 will create divide by zero problem. */
  314         if (resources.factor == 0) {
  315         fprintf(stderr, "-factor of 0.0 is not allowed, nothing "
  316             "will plot!\n");
  317         exit(1);
  318         }
  319     }
  320     if (want_snmp) {
  321         my_snmp.peername = (char*)resources.peername;
  322         my_snmp.community = (char*)resources.community;
  323         my_snmp.oid = (char*)resources.oid;
  324         my_snmp.factor = resources.factor;
  325         my_snmp.delta = resources.delta;
  326 
  327         XtAddCallback(load, XtNgetValue, GetLoadPoint_SNMP, 
  328               (XtPointer)&my_snmp);
  329     } 
  330     else {
  331         XtAddCallback(load, XtNgetValue, GetLoadPoint, NULL);
  332     }
  333 
  334         XtRealizeWidget (toplevel);
  335         wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
  336                         False);
  337         (void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
  338                     &wm_delete_window, 1);
  339     }
  340     XtAppMainLoop(app_con);
  341 }
  342 
  343 static unsigned long    current_leds;
  344 
  345 static void
  346 ClearLights (dpy)
  347     Display *dpy;
  348 {
  349     XKeyboardControl    cntrl;
  350 
  351     cntrl.led_mode = LedModeOff;
  352     XChangeKeyboardControl (dpy, KBLedMode, &cntrl);
  353     current_leds = 0;
  354 }
  355 
  356 static void
  357 SetLights (data, timer)
  358     XtPointer       data;
  359     XtIntervalId    *timer;
  360 {
  361     Widget      toplevel;
  362     Display     *dpy;
  363     double      value;
  364     unsigned long   new_leds, change, bit;
  365     int         i;
  366     XKeyboardControl    cntrl;
  367 
  368     toplevel = (Widget) data;
  369     dpy = XtDisplay (toplevel);
  370     GetLoadPoint (toplevel, (XtPointer) 0, (XtPointer) &value);
  371     new_leds = (1 << (int) (value + 0.1)) - 1;
  372     change = new_leds ^ current_leds;
  373     i = 1;
  374     bit = 1;
  375     while (current_leds != new_leds)
  376     {
  377     if (change & bit)
  378     {
  379         cntrl.led = i;
  380         cntrl.led_mode = new_leds & bit ? LedModeOn : LedModeOff;
  381         XChangeKeyboardControl (dpy, KBLed|KBLedMode, &cntrl);
  382         current_leds ^= bit;
  383     }
  384     i++;
  385     bit <<= 1;
  386     }
  387     XtAppAddTimeOut(XtWidgetToApplicationContext(toplevel), light_update,
  388             SetLights, data);
  389 }
  390 
  391 static void quit (w, event, params, num_params)
  392     Widget w;
  393     XEvent *event;
  394     String *params;
  395     Cardinal *num_params;
  396 {
  397     if (event->type == ClientMessage &&
  398         event->xclient.data.l[0] != wm_delete_window) {
  399         XBell (XtDisplay(w), 0);
  400         return;
  401     }
  402     if (resources.use_lights)
  403     ClearLights (XtDisplay (w));
  404     XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  405     exit (0);
  406 }