monitor.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2002 by Contributors to the monit codebase. 
00003  * All Rights Reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00018  */
00019 
00020 
00021 #ifndef MONITOR_H
00022 #define MONITOR_H
00023 
00024 #include <config.h>
00025 
00026 #ifdef HAVE_SYS_TYPES_H
00027 #include <sys/types.h>
00028 #endif
00029 
00030 #ifdef HAVE_SYS_STAT_H
00031 #include <sys/stat.h>
00032 #endif
00033 
00034 #include <signal.h>
00035 #include <pthread.h>
00036 #include <stdarg.h>
00037 
00038 
00039 #define VERSION            PACKAGE_VERSION
00040 #define MONITRC            "monitrc"
00041 #define TIMEFORMAT         "%Z %b %e %T"
00042 #define STRERROR            strerror(errno)
00043 #define STRLEN             256
00044 #define ARGMAX             64
00045 /* Set log file mode: "-rw-rw-r--" */
00046 #define LOGMASK            0112
00047 /* Set pid file mode: "-rw-r--r--" */
00048 #define PIDMASK            0122
00049 #define MYPIDDIR           PIDDIR
00050 #define MYPIDFILE          "monit.pid"
00051 
00052 #define LOCALHOST          "localhost"
00053 #define SMTP_PORT          25
00054 
00055 #define SMTP_TIMEOUT       30
00056 #define CHECK_TIMEOUT      30
00057 
00058 #define START_HTTP         1
00059 #define STOP_HTTP          2
00060 
00061 #define TRUE               1
00062 #define FALSE              0
00063 
00064 #define MAXIMUM(x,y)       ((x) > (y) ? (x) : (y))
00065 #define is(a,b)        ((a&&b)?!strcasecmp(a, b):0)
00066 
00068 typedef void Sigfunc(int);
00069 Sigfunc *signal(int signo, Sigfunc * func);
00070 #if defined(SIG_IGN) && !defined(SIG_ERR)
00071 #define SIG_ERR ((Sigfunc *)-1)
00072 #endif
00073 
00076 #define LOCK(mutex) do { pthread_mutex_t *_yymutex = &(mutex); \
00077         pthread_mutex_lock(_yymutex);
00078 #define END_LOCK pthread_mutex_unlock(_yymutex); } while (0)
00079 
00080 
00083 #define NEW(p) (xcalloc(1, (long)sizeof *(p)))
00084 
00085 
00087 struct myrun {
00088   char *controlfile;                
00089   char *logfile;                         
00090   char *localhostname;                      
00091   char *mailserver;              
00092   char *pidfile;                                  
00093   int  debug;                   
00094   int  use_syslog;                          
00095   int  dolog;       
00096   int  isdaemon;                 
00097   int  polltime;        
00098   int  dohttpd;                    
00099   int  httpdport;                    
00100   int  validate;           
00101   int  Init;               
00102   int  doprocess;                 
00103   char *bind_addr;                  
00104   mode_t umask;                
00106   double loadavg[3];                                
00109   struct myenvironment {
00110     char *user;             
00111     char *home;                                    
00112     char *cwd;                                
00113   } Env;
00115   struct myautentication {
00116     int  defined;              
00117     char *uname;                 
00118     char *passwd;                      
00119   } Auth;
00121   struct myformat {
00122     char *from;                          
00123     char *subject;                            
00124     char *message;                            
00125   } MailFormat;
00126                                           
00127   pthread_mutex_t mutex;    
00129 };
00130 
00132 typedef struct myport {
00133   volatile int socket;                       
00134   int  type;                  
00135   int  family;            
00136   char *hostname;                                     
00137   int  port;                                                 
00138   char *request;                              
00139   char *pathname;                   
00140   char *address;               
00143   struct myprotocol {
00144     const char *name;                                     
00145     int(*check)(struct myport *);         
00146   } *protocol;
00147   
00149   struct myport *next;                               
00150 } *Port_T;
00151 
00153 typedef struct myprotocol *Protocol_T;
00154 
00156 typedef struct mychecksum {
00157   char *file;                  
00158   char *md5;                       
00161   struct mychecksum *next;                       
00162 } *Checksum_T;
00163 
00165 typedef struct mymail {
00166   char *to;                         
00167   char *from;                                     
00168   char *subject;                                       
00169   char *message;                                       
00170   char *opt_message;                 
00171   int  alert_on_timeout;       
00172   int  alert_on_restart;  
00173   int  alert_on_checksum;    
00174   int  alert_on_resource;     
00175   int  alert_on_stop;          
00178   struct mymail *next;                          
00179 } *Mail_T;
00180 
00182 typedef struct myresource {
00183   int  resource_id;                              
00184   long int limit;                                 
00185   int  operator;                                    
00186   int  cycle;                                     
00187   int  max_cycle;                                   
00188   int  action;                               
00190   struct myresource *next;
00191 } *Resource_T;
00192 
00194 typedef struct myprocinfo{
00195   int  pid;
00196   int  status_flag;
00197   long mem_kbyte;    
00198   int  mem_percent;                                      
00199   int  cpu_percent;                                      
00200   /* private for calculating cpu_percent */
00201   double time_prev;                                        
00202   long  cputime;                                           
00203   long  cputime_prev;                                      
00204 } *ProcInfo_T;
00205 
00211 typedef struct mycommand {
00212   char *arg[ARGMAX];                             
00213   int length;                         
00214 } *Command_T;
00215 
00217 typedef struct myprocess {
00218   char *name;                                  
00219   char *pidfile;                            
00220   char *group;                                       
00221   Command_T start;                    
00222   Command_T stop;                      
00223   int  do_validate;    
00224   int  ncycle;                          
00225   int  nstart;           
00226   int  to_start;                                  
00227   int  to_cycle;                                  
00228   int  every;                        
00229   int  nevery;          
00230   int  has_checksum_error;     
00231   int  def_every;              
00232   int  def_timeout;          
00233   int  def_checksum;        
00234   int  def_procinfo;        
00236   Port_T portlist;                   
00237   Checksum_T checksumlist;          
00238   Mail_T maillist;                       
00239   Resource_T resourcelist;                           
00241   ProcInfo_T procinfo;                       
00244   struct myprocess *next;                         
00245 } *Process_T;
00246 
00247 
00248 /* ------ Global variables ------------------------------------------------- */
00249 Process_T processlist;                
00250 char   *prog;                                  
00251 struct myrun Run;                      
00252 int    have_tty;                                 
00253 char   *mygroup;                              
00256 /* Public prototypes */
00257 void  error(const char *, ...);
00258 char *trim(char *);
00259 char *ltrim(char *);
00260 char *rtrim(char *);
00261 char *trim_quotes(char *);
00262 int   starts_with(char *,char *);
00263 void  handle_string_escapes(char *);
00264 void  chomp(char *);
00265 int   exist_process(char *);
00266 Process_T get_process(char *);
00267 void  printrunlist();
00268 void  printprocess(Process_T);
00269 void  printprocesslist();
00270 pid_t get_pid(char *);
00271 int   is_process_running(Process_T);
00272 int   is_strdefined(char *);
00273 int   isreg_file(char *);
00274 char *stripfilename(char*);
00275 int   exist_file(char *);
00276 time_t file_changedtime(char *file);
00277 char *get_ctime();
00278 char *get_RFC1123date(long *);
00279 char *get_uptime(long delta);
00280 char *get_process_uptime(char *);
00281 char *get_md5sum(char *);
00282 int   check_md5(char *, char *);
00283 char *url_encode(char *uri);
00284 char *get_basic_authentication_header();
00285 int   set_md5sum(char **, char *);
00286 int   parse(char *);
00287 void  start();
00288 void  start_group(char *);
00289 void  start_process(Process_T);
00290 void  dstart_process(char *);
00291 void  stop();
00292 void  stop_group(char *);
00293 void  stop_process(Process_T);
00294 void  dstop_process(char *);
00295 void  spawn(Process_T, Command_T);
00296 void  status();
00297 void  status_group(char *);
00298 int   log_init() ;
00299 void  log(const char *format, ...) ;
00300 void  log_close();
00301 int   get_log_fd();
00302 void  validate() ;
00303 void  daemonize() ;
00304 void  gc();
00305 void  gc_process(Process_T*);
00306 void  gc_mail_list(Mail_T*);
00307 void  init_files();
00308 void  re_init_files();
00309 void  set_file_timestamp();
00310 void  finalize_files();
00311 char *find_rcfile();    
00312 int   create_pidfile(char *);
00313 int   is_rcfile_changed();
00314 int   check_rcfile(char *);
00315 int   kill_daemon();
00316 int   exist_daemon(); 
00317 void  sendmail(Mail_T);
00318 int   sock_msg(int sock, char *, ...);
00319 void  init_env();
00320 void *xmalloc (int);
00321 void *xcalloc(long, long);
00322 char *xstrdup(const char *);
00323 void *xresize(void *, long);
00324 void  set_alarm_handler(void *);
00325 void  monit_http(int);
00326 int   check_httpd();
00327 int   can_http();
00328 char *format(const char *, va_list);
00329 
00330 #endif