monitor.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C), 2000-2003 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 MODE_ACTIVE        0
00068 #define MODE_PASSIVE       1
00069 #define MODE_MANUAL        2
00070 
00071 #define OPERATOR_GREATER   0
00072 #define OPERATOR_LESS      1
00073 #define OPERATOR_EQUAL     2
00074 #define OPERATOR_NOTEQUAL  3
00075 
00076 #define TIME_SECOND    1
00077 #define TIME_MINUTE    60
00078 #define TIME_HOUR      3600
00079 #define TIME_DAY       86400
00080 
00081 #define ACTION_ALERT       1
00082 #define ACTION_RESTART     2
00083 #define ACTION_STOP        3
00084 
00085 #define MAXIMUM(x,y)       ((x) > (y) ? (x) : (y))
00086 #define is(a,b)        ((a&&b)?!strcasecmp(a, b):0)
00087 
00088 
00090 typedef void Sigfunc(int);
00091 Sigfunc *signal(int signo, Sigfunc * func);
00092 #if defined(SIG_IGN) && !defined(SIG_ERR)
00093 #define SIG_ERR ((Sigfunc *)-1)
00094 #endif
00095 
00098 #define LOCK(mutex) do { pthread_mutex_t *_yymutex = &(mutex); \
00099         pthread_mutex_lock(_yymutex);
00100 #define END_LOCK pthread_mutex_unlock(_yymutex); } while (0)
00101 
00102 
00105 #define NEW(p) (xcalloc(1, (long)sizeof *(p)))
00106 
00109 #define ASSERT(e) if(!(e)) { error("AssertException: at %s:%d\naborting..\n", \
00110     __FILE__, __LINE__); abort(); }
00111 
00112 /* --------------------------------------------------------- Data structures */
00113 
00115 struct myrun {
00116   char *controlfile;                
00117   char *logfile;                         
00118   char *localhostname;                      
00119   char *mailserver;              
00120   char *pidfile;                                  
00121   char *mygroup;                              
00122   int  have_tty;                                 
00123   int  debug;                   
00124   int  use_syslog;                          
00125   int  dolog;       
00126   int  isdaemon;                 
00127   int  polltime;        
00128   int  dohttpd;                    
00129   int  httpdssl;                     
00130   char *httpsslpem;                       
00131   int  clientssl;   
00132   char *httpsslclientpem;      
00133   int  servicessl;                 
00134   int  allowselfcert;   
00135   int  httpdport;                    
00136   int  validate;           
00137   int  init;               
00138   int  doprocess;                 
00139   char *bind_addr;                  
00140   int  doreload;             
00141   mode_t umask;                
00142   int  testing;   
00144   double loadavg[3];                                
00147   struct myenvironment {
00148     char *user;             
00149     char *home;                                    
00150     char *cwd;                                
00151   } Env;
00153   struct myautentication {
00154     int  defined;              
00155     char *uname;                 
00156     char *passwd;                      
00157   } Auth;
00159   struct myformat {
00160     char *from;                          
00161     char *subject;                            
00162     char *message;                            
00163   } MailFormat;
00164                                           
00165   pthread_mutex_t mutex;    
00167 };
00168 
00170 typedef struct myport {
00171   volatile int socket;                       
00172   int  type;                  
00173   int  family;            
00174   char *hostname;                                     
00175   int  port;                                                 
00176   char *request;                              
00177   char *pathname;                   
00178   char *address;               
00180   ssl_connection * ssl;
00181   char *certmd5;
00183   struct myprotocol {
00184     const char *name;                                     
00185     int(*check)(struct myport *);         
00186   } *protocol;
00187   
00189   struct myport *next;                               
00190 } *Port_T;
00191 
00193 typedef struct myprotocol *Protocol_T;
00194 
00195 typedef struct mydependant {
00196   char *dependant;                           
00198   struct mydependant *next;           
00199 } *Dependant_T;
00200 
00202 typedef struct mychecksum {
00203   char *file;                  
00204   char *md5;                       
00207   struct mychecksum *next;                       
00208 } *Checksum_T;
00209 
00211 typedef struct mymail {
00212   char *to;                         
00213   char *from;                                     
00214   char *subject;                                       
00215   char *message;                                       
00216   char *opt_message;                 
00217   int  alert_on_timeout;       
00218   int  alert_on_restart;  
00219   int  alert_on_checksum;    
00220   int  alert_on_resource;     
00221   int  alert_on_stop;          
00222   int  alert_on_timestamp;  
00225   struct mymail *next;                          
00226 } *Mail_T;
00227 
00229 typedef struct myresource {
00230   int  resource_id;                              
00231   long int limit;                                 
00232   int  operator;                                    
00233   int  cycle;                                     
00234   int  max_cycle;                                   
00235   int  action;                                
00237   struct myresource *next;
00238 } *Resource_T;
00239 
00241 typedef struct mytimestamp {
00242   char *pathname;                    
00243   int  operator;                    
00244   int  time;                        
00245   int  action;                    
00247   struct mytimestamp *next;
00248 } *Timestamp_T;
00249 
00251 typedef struct myprocinfo {
00252   int  pid;
00253   int  status_flag;
00254   long mem_kbyte;    
00255   int  mem_percent;                                      
00256   int  cpu_percent;                                      
00257   /* private for calculating cpu_percent */
00258   double time_prev;                                        
00259   long  cputime;                                           
00260   long  cputime_prev;                                      
00261 } *ProcInfo_T;
00262 
00268 typedef struct mycommand {
00269   char *arg[ARGMAX];                             
00270   int length;                         
00271 } *Command_T;
00272 
00274 typedef struct myprocess {
00275   char *name;                                  
00276   char *pidfile;                            
00277   char *group;                                       
00278   Command_T start;                    
00279   Command_T stop;                      
00280   int  do_validate;    
00281   int  mode;                            
00282   int  ncycle;                          
00283   int  nstart;           
00284   int  to_start;                                  
00285   int  to_cycle;                                  
00286   int  every;                        
00287   int  nevery;          
00288   int  has_checksum_error;     
00289   int  def_every;              
00290   int  def_timeout;          
00291   int  def_checksum;        
00292   int  def_procinfo;        
00293   int  visited;      
00294   int  depend_visited;
00296   Port_T portlist;                   
00297   Checksum_T checksumlist;          
00298   Mail_T maillist;                       
00299   Resource_T resourcelist;                           
00300   Dependant_T dependantlist;                      
00301   Timestamp_T timestamplist;                       
00302   ProcInfo_T procinfo;                        
00305   pthread_mutex_t mutex;          
00306   struct myprocess *next;                         
00307   struct myprocess *next_depend;           
00308 } *Process_T;
00309 
00310 
00311 /* ------ Global variables ------------------------------------------------- */
00312 Process_T processlist;                
00313 char   *prog;                                  
00314 struct myrun Run;                      
00317 /* Public prototypes */
00318 void  error(const char *, ...);
00319 char *trim(char *);
00320 char *ltrim(char *);
00321 char *rtrim(char *);
00322 char *trim_quotes(char *);
00323 int   starts_with(char *,char *);
00324 void  handle_string_escapes(char *);
00325 void  chomp(char *);
00326 int   exist_process(char *);
00327 Process_T get_process(char *);
00328 void  printrunlist();
00329 void  printprocess(Process_T);
00330 void  printprocesslist();
00331 pid_t get_pid(char *);
00332 int   is_process_running(Process_T);
00333 int   is_strdefined(char *);
00334 int   isreg_file(char *);
00335 char *stripfilename(char*);
00336 int   exist_file(char *);
00337 char *get_ctime();
00338 char *get_RFC1123date(long *);
00339 char *get_uptime(long delta);
00340 char *get_process_uptime(char *);
00341 char *get_md5sum(char *);
00342 int   check_md5(char *, char *);
00343 char *url_encode(char *uri);
00344 char *get_basic_authentication_header();
00345 int   set_md5sum(char **, char *);
00346 int   parse(char *);
00347 void  control(char *);
00348 void  control_group(char *, char *);
00349 void  d_check_process(char *, char *);
00350 void  check_process(char *, char *);
00351 void  setup_dependants();
00352 void  reset_depend();
00353 void  spawn(Process_T, Command_T);
00354 void  status();
00355 void  status_group(char *);
00356 int   log_init() ;
00357 void  log(const char *format, ...) ;
00358 void  log_close();
00359 int   get_log_fd();
00360 void  validate() ;
00361 void  daemonize() ;
00362 void  gc();
00363 void  gc_process(Process_T*);
00364 void  gc_mail_list(Mail_T*);
00365 void  init_files();
00366 time_t get_timestamp(char *, mode_t);
00367 void  finalize_files();
00368 char *find_rcfile();    
00369 int   create_pidfile(char *);
00370 int   check_rcfile(char *);
00371 int   kill_daemon(int);
00372 int   exist_daemon(); 
00373 void  sendmail(Mail_T);
00374 int   sock_msg(int sock, char *, ...);
00375 void  init_env();
00376 void *xmalloc (int);
00377 void *xcalloc(long, long);
00378 char *xstrdup(const char *);
00379 void *xresize(void *, long);
00380 void  set_alarm_handler(void *);
00381 void  monit_http(int);
00382 int   check_httpd();
00383 int   can_http();
00384 char *format(const char *, va_list);
00385 void  redirect_stdfd();
00386 pid_t getpgid(pid_t);
00387 int   check_file(char *, char *, int);
00388 
00389 #endif