Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

rmSubagent.c

Go to the documentation of this file.
00001 #include <ucd-snmp/ucd-snmp-config.h>
00002 #ifdef HAVE_NETINET_IN_H
00003 #include <netinet/in.h>
00004 #endif
00005 #include <ucd-snmp/ucd-snmp-includes.h>
00006 #include <ucd-snmp/ucd-snmp-agent-includes.h>
00007 #include <signal.h>
00008 
00009 #include <ucd-snmp/agent_trap.h>
00010 
00011 #include "rmClient.h"
00012 
00013 #include <unistd.h>
00014 #include <stdio.h>
00015 #include <sys/types.h>
00016 #include <sys/stat.h>
00017 #include <sys/file.h>
00018 #include <fcntl.h>
00019 #include <stdlib.h>
00020 #include <string.h>
00021 
00022 #include "RMSystemInfo.h"
00023 #include "RMGenericConfig.h"
00024 #include "RMThresholdConfig.h"
00025 #include "RMWatermarkConfig.h"
00026 #include "RMLeakyBucketConfig.h"
00027 #include "RMGenericControl.h"
00028 #include "RMThresholdControl.h"
00029 #include "RMLeakyBucketControl.h"
00030 #include "RMMonitors.h"
00031 #include "ResourceMonitor/MonitoringEventSchema.h"
00032 
00033 static int keep_running;
00034 
00035 RETSIGTYPE
00036 stop_server(int a) {
00037     keep_running = 0;
00038 }
00039 
00040 void daemonize();
00041 
00042 void catchNotificationEvent(int signo, siginfo_t *info, void *ignored);
00043 
00044 void
00045 catchNotificationEvent(int signo, siginfo_t *info, void *ignored)
00046 {
00047   posix_log_recid_t recid;
00048   unsigned long monid;
00049 
00050   posix_log_siginfo_getrecid(info, ignored, &recid);
00051   monid = (unsigned long) info->si_int;
00052 
00053   addEvent(recid, monid);
00054 }
00055 
00056 int main () {
00057 
00058   int agentx_subagent=1; /* change this if you're a master agent */
00059 
00060   struct sigaction act;
00061 
00062   memset(&act, 0, sizeof(act));
00063   act.sa_flags = SA_SIGINFO | SA_RESTART;
00064   act.sa_handler = (void (*) (int)) catchNotificationEvent;
00065   sigaction(SIGRTMIN + 1, &act, NULL);
00066 
00067 
00068   daemonize();
00069 
00070   /* print log errors to stderr */
00071   snmp_enable_stderrlog();
00072 
00073   /* we're an agentx subagent? */
00074   if (agentx_subagent) {
00075     /* make us a agentx client. */
00076     ds_set_boolean(DS_APPLICATION_ID, DS_AGENT_ROLE, 1);
00077   }
00078 
00079   /* initialize the agent library */
00080   init_agent("rmSubagent");
00081 
00082   initRMClient();
00083 
00084   init_RMSystemInfo();
00085   init_RMGenericConfig();
00086   init_RMThresholdConfig();
00087   init_RMWatermarkConfig();
00088   init_RMLeakyBucketConfig();
00089   init_RMGenericControl();
00090   init_RMThresholdControl();
00091   init_RMLeakyBucketControl();
00092   init_RMMonitors();
00093   
00094   /* ustMain will be used to read ustMain.conf files. */
00095   init_snmp("rmSubagent");
00096 
00097   /* If we're going to be a snmp master agent */
00098   if (!agentx_subagent)
00099     init_master_agent(161, NULL, NULL);  /* open port 161 (UDP:snmp) */
00100 
00101   /* In case we recevie a request to stop (kill -TERM or kill -INT) */
00102   keep_running = 1;
00103   signal(SIGTERM, stop_server);
00104   signal(SIGINT, stop_server);
00105 
00106   /* you're main loop here... */
00107   while(keep_running) {
00108     /* if you use select(), see snmp_select_info() in snmp_api(3) */
00109     /*     --- OR ---  */
00110     agent_check_and_process(1); /* 0 == don't block */
00111 
00112     processEvents();
00113 
00114   }
00115 
00116 
00117   /* at shutdown time */
00118   snmp_shutdown("rmSubagent");
00119   shutdownRMClient();
00120 
00121   return 0;
00122 }
00123 
00124 void
00125 daemonize()
00126 {
00127   pid_t pid;
00128 
00129   int dpid;
00130   FILE *pidfile;
00131   int fd;
00132 
00133   char * pfile;
00134 
00135   pfile = "/var/run/rmsubagentd.pid";
00136 
00137 
00138   if((pid = fork()) < 0) {
00139     fprintf(stderr, "can not fork child process. ");
00140     exit(1);
00141   }
00142   else if (pid > 0) {
00143     exit(0);
00144   }
00145 
00146   (void)setpgrp();
00147 
00148   (void)signal(SIGHUP, SIG_IGN);
00149 
00150   if ((pid = fork()) < 0) {
00151     fprintf(stderr, " can not fork the second child process.");
00152     exit(1);
00153   }
00154   else if(pid > 0) {
00155     exit(0);
00156   }
00157 
00158   chdir("/");
00159   umask(0);
00160 
00161   if ((fd = open(pfile, O_RDWR|O_CREAT, 0644)) == -1) 
00162     return;
00163 
00164   if(( pidfile = fdopen(fd, "r+")) == NULL) {
00165     close(fd);
00166     return;
00167   }
00168 
00169   dpid = getpid();
00170   flock(fd, LOCK_EX);
00171   fprintf(pidfile, "%d\n", dpid);
00172   flock(fd, LOCK_UN);
00173   fclose(pidfile);
00174   close(fd);
00175 }

Generated on Tue Sep 10 16:46:33 2002 for ResourceMonitorSNMPSubagent by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002