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 #include <stdlib.h>
00038 
00039 #include "ssl.h"
00040 
00041 #define VERSION            PACKAGE_VERSION
00042 #define MONITRC            "monitrc"
00043 #define TIMEFORMAT         "%Z %b %e %T"
00044 #define STRERROR            strerror(errno)
00045 #define STRLEN             256
00046 #define ARGMAX             64
00047 /* Set log file mode: "-rw-rw-r--" */
00048 #define LOGMASK            0112
00049 /* Set pid file mode: "-rw-r--r--" */
00050 #define PIDMASK            0122
00051 #define MYPIDDIR           PIDDIR
00052 #define MYPIDFILE          "monit.pid"
00053 
00054 #define LOCALHOST          "localhost"
00055 #define SMTP_PORT          25
00056 
00057 #define SMTP_TIMEOUT       30
00058 #define CHECK_TIMEOUT      30
00059 #define SSL_TIMEOUT        5
00060 
00061 #define START_HTTP         1
00062 #define STOP_HTTP          2
00063 
00064 #define TRUE               1
00065 #define FALSE              0
00066 
00067 #define FORCE_CONTROL      0
00068 #define HTTP_CONTROL       1
00069 
00070 #define MODE_ACTIVE        0
00071 #define MODE_PASSIVE       1
00072 #define MODE_MANUAL        2
00073 
00074 #define OPERATOR_GREATER   0
00075 #define OPERATOR_LESS      1
00076 #define OPERATOR_EQUAL     2
00077 #define OPERATOR_NOTEQUAL  3
00078 
00079 #define TIME_SECOND    1
00080 #define TIME_MINUTE    60
00081 #define TIME_HOUR      3600
00082 #define TIME_DAY       86400
00083 
00084 #define ACTION_ALERT       1
00085 #define ACTION_RESTART     2
00086 #define ACTION_STOP        3
00087 
00088 #define MAXIMUM(x,y)       ((x) > (y) ? (x) : (y))
00089 #define is(a,b)        ((a&&b)?!strcasecmp(a, b):0)
00090 
00091 
00093 typedef void Sigfunc(int);
00094 Sigfunc *signal(int signo, Sigfunc * func);
00095 #if defined(SIG_IGN) && !defined(SIG_ERR)
00096 #define SIG_ERR ((Sigfunc *)-1)
00097 #endif
00098 
00101 #define LOCK(mutex) do { pthread_mutex_t *_yymutex = &(mutex); \
00102         pthread_mutex_lock(_yymutex);
00103 #define END_LOCK pthread_mutex_unlock(_yymutex); } while (0)
00104 
00105 
00108 #define NEW(p) (xcalloc(1, (long)sizeof *(p)))
00109 
00112 #define ASSERT(e) if(!(e)) { error("AssertException: at %s:%d\naborting..\n", \
00113     __FILE__, __LINE__); abort(); }
00114 
00115 /* --------------------------------------------------------- Data structures */
00116 
00118 struct myrun {
00119   char *controlfile;                
00120   char *logfile;                         
00121   char *localhostname;                      
00122   char *mailserver;              
00123   char *pidfile;                                  
00124   char *mygroup;                              
00125   int  have_tty;                                 
00126   int  debug;                   
00127   int  use_syslog;                          
00128   int  dolog;       
00129   int  isdaemon;                 
00130   int  polltime;        
00131   int  dohttpd;                    
00132   int  httpdssl;                     
00133   char *httpsslpem;                       
00134   int  clientssl;   
00135   char *httpsslclientpem;      
00136   int  servicessl;                 
00137   int  allowselfcert;   
00138   int  httpdport;                    
00139   int  validate;           
00140   int  init;               
00141   int  doprocess;                 
00142   char *bind_addr;                  
00143   mode_t umask;                
00144   time_t timestamp;                
00146   double loadavg[3];                                
00149   struct myenvironment {
00150     char *user;             
00151     char *home;                                    
00152     char *cwd;                                
00153   } Env;
00155   struct myautentication {
00156     int  defined;              
00157     char *uname;                 
00158     char *passwd;                      
00159   } Auth;
00161   struct myformat {
00162     char *from;                          
00163     char *subject;                            
00164     char *message;                            
00165   } MailFormat;
00166                                           
00167   pthread_mutex_t mutex;    
00169 };
00170 
00172 typedef struct myport {
00173   volatile int socket;                       
00174   int  type;                  
00175   int  family;            
00176   char *hostname;                                     
00177   int  port;                                                 
00178   char *request;                              
00179   char *pathname;                   
00180   char *address;               
00182   ssl_connection * ssl;
00183   char *certmd5;
00185   struct myprotocol {
00186     const char *name;                                     
00187     int(*check)(struct myport *);         
00188   } *protocol;
00189   
00191   struct myport *next;                               
00192 } *Port_T;
00193 
00195 typedef struct myprotocol *Protocol_T;
00196 
00197 typedef struct mydependant {
00198   char *dependant;                           
00200   struct mydependant *next;           
00201 } *Dependant_T;
00202 
00204 typedef struct mychecksum {
00205   char *file;                  
00206   char *md5;                       
00209   struct mychecksum *next;                       
00210 } *Checksum_T;
00211 
00213 typedef struct mymail {
00214   char *to;                         
00215   char *from;                                     
00216   char *subject;                                       
00217   char *message;                                       
00218   char *opt_message;                 
00219   int  alert_on_timeout;       
00220   int  alert_on_restart;  
00221   int  alert_on_checksum;    
00222   int  alert_on_resource;     
00223   int  alert_on_stop;          
00224   int  alert_on_timestamp;  
00227   struct mymail *next;                          
00228 } *Mail_T;
00229 
00231 typedef struct myresource {
00232   int  resource_id;                              
00233   long int limit;                                 
00234   int  operator;                                    
00235   int  cycle;                                     
00236   int  max_cycle;                                   
00237   int  action;                                
00239   struct myresource *next;
00240 } *Resource_T;
00241 
00243 typedef struct mytimestamp {
00244   char *pathname;                    
00245   int  operator;                    
00246   int  time;                        
00247   int  action;                    
00249   struct mytimestamp *next;
00250 } *Timestamp_T;
00251 
00253 typedef struct myprocinfo {
00254   int  pid;
00255   int  status_flag;
00256   long mem_kbyte;    
00257   int  mem_percent;                                      
00258   int  cpu_percent;                                      
00259   /* private for calculating cpu_percent */
00260   double time_prev;                                        
00261   long  cputime;                                           
00262   long  cputime_prev;                                      
00263 } *ProcInfo_T;
00264 
00270 typedef struct mycommand {
00271   char *arg[ARGMAX];                             
00272   int length;                         
00273 } *Command_T;
00274 
00276 typedef struct myprocess {
00277   char *name;                                  
00278   char *pidfile;                            
00279   char *group;                                       
00280   Command_T start;                    
00281   Command_T stop;                      
00282   int  do_validate;    
00283   int  mode;                            
00284   int  ncycle;                          
00285   int  nstart;           
00286   int  to_start;                                  
00287   int  to_cycle;                                  
00288   int  every;                        
00289   int  nevery;          
00290   int  has_checksum_error;     
00291   int  def_every;              
00292   int  def_timeout;          
00293   int  def_checksum;        
00294   int  def_procinfo;        
00295   int  visited;      
00296   int  depend_visited;
00298   Port_T portlist;                   
00299   Checksum_T checksumlist;          
00300   Mail_T maillist;                       
00301   Resource_T resourcelist;                           
00302   Dependant_T dependantlist;                      
00303   Timestamp_T timestamplist;                       
00305   ProcInfo_T procinfo;                       
00308   struct myprocess *next;                         
00309   struct myprocess *next_depend;           
00310 } *Process_T;
00311 
00312 
00313 /* ------ Global variables ------------------------------------------------- */
00314 Process_T processlist;                
00315 char   *prog;                                  
00316 struct myrun Run;                      
00319 /* Public prototypes */
00320 void  error(const char *, ...);
00321 char *trim(char *);
00322 char *ltrim(char *);
00323 char *rtrim(char *);
00324 char *trim_quotes(char *);
00325 int   starts_with(char *,char *);
00326 void  handle_string_escapes(char *);
00327 void  chomp(char *);
00328 int   exist_process(char *);
00329 Process_T get_process(char *);
00330 void  printrunlist();
00331 void  printprocess(Process_T);
00332 void  printprocesslist();
00333 pid_t get_pid(char *);
00334 int   is_process_running(Process_T);
00335 int   is_strdefined(char *);
00336 int   isreg_file(char *);
00337 char *stripfilename(char*);
00338 int   exist_file(char *);
00339 char *get_ctime();
00340 char *get_RFC1123date(long *);
00341 char *get_uptime(long delta);
00342 char *get_process_uptime(char *);
00343 char *get_md5sum(char *);
00344 int   check_md5(char *, char *);
00345 char *url_encode(char *uri);
00346 char *get_basic_authentication_header();
00347 int   set_md5sum(char **, char *);
00348 int   parse(char *);
00349 void  control(char *, int);
00350 void  control_group(char *, char *, int);
00351 void  d_check_process(char *, char *);
00352 void  check_process(char *, char *, int);
00353 void  setup_dependants();
00354 void  reset_depend();
00355 void  spawn(Process_T, Command_T);
00356 void  status();
00357 void  status_group(char *);
00358 int   log_init() ;
00359 void  log(const char *format, ...) ;
00360 void  log_close();
00361 int   get_log_fd();
00362 void  validate() ;
00363 void  daemonize() ;
00364 void  gc();
00365 void  gc_process(Process_T*);
00366 void  gc_mail_list(Mail_T*);
00367 void  init_files();
00368 time_t get_timestamp(char *, mode_t);
00369 void  finalize_files();
00370 char *find_rcfile();    
00371 int   create_pidfile(char *);
00372 int   is_rcfile_changed();
00373 int   check_rcfile(char *);
00374 int   kill_daemon();
00375 int   exist_daemon(); 
00376 void  sendmail(Mail_T);
00377 int   sock_msg(int sock, char *, ...);
00378 void  init_env();
00379 void *xmalloc (int);
00380 void *xcalloc(long, long);
00381 char *xstrdup(const char *);
00382 void *xresize(void *, long);
00383 void  set_alarm_handler(void *);
00384 void  monit_http(int);
00385 int   check_httpd();
00386 int   can_http();
00387 char *format(const char *, va_list);
00388 void  redirect_stdfd();
00389 pid_t getpgid(pid_t);
00390 int   check_file(char *, char *, int);
00391 
00392 #endif