monitor.h File Reference

Go to the source code of this file.

Data Structures

struct  mychecksum
 Defines a checksum object. More...

struct  mycommand
 Defines a command with ARGMAX optional arguments. More...

struct  mydependant
struct  mymail
 Defines a mailinglist object. More...

struct  myport
 Defines a port object. More...

struct  myprotocol
struct  myprocess
 Defines process data. More...

struct  myprocinfo
 Defines procfs (or other mechanism) data. More...

struct  myresource
 Defines resource data. More...

struct  myformat
 User selected standard mail format. More...

struct  myautentication
 An object holding Basic Authentication information. More...

struct  myenvironment
 An object holding program relevant "environment" data, see; env.c. More...

struct  myrun
 Defines data for application runtime. More...

struct  mytimestamp
 Defines timestamp object. More...


Defines

#define VERSION   PACKAGE_VERSION
#define MONITRC   "monitrc"
#define TIMEFORMAT   "%Z %b %e %T"
#define STRERROR   strerror(errno)
#define STRLEN   256
#define ARGMAX   64
#define LOGMASK   0112
#define PIDMASK   0122
#define MYPIDDIR   PIDDIR
#define MYPIDFILE   "monit.pid"
#define LOCALHOST   "localhost"
#define SMTP_PORT   25
#define SMTP_TIMEOUT   30
#define CHECK_TIMEOUT   30
#define SSL_TIMEOUT   5
#define START_HTTP   1
#define STOP_HTTP   2
#define TRUE   1
#define FALSE   0
#define FORCE_CONTROL   0
#define HTTP_CONTROL   1
#define MODE_ACTIVE   0
#define MODE_PASSIVE   1
#define MODE_MANUAL   2
#define OPERATOR_GREATER   0
#define OPERATOR_LESS   1
#define OPERATOR_EQUAL   2
#define OPERATOR_NOTEQUAL   3
#define TIME_SECOND   1
#define TIME_MINUTE   60
#define TIME_HOUR   3600
#define TIME_DAY   86400
#define ACTION_ALERT   1
#define ACTION_RESTART   2
#define ACTION_STOP   3
#define MAXIMUM(x, y)   ((x) > (y) ? (x) : (y))
#define is(a, b)   ((a&&b)?!strcasecmp(a, b):0)
#define LOCK(mutex)
 ------------------------------------------------- Synchronization macros

#define END_LOCK   pthread_mutex_unlock(_yymutex); } while (0)
#define NEW(p)   (xcalloc(1, (long)sizeof *(p)))
 ----------------------------------------------- Object Constructor macro

#define ASSERT(e)
 ------------------------------------------ Simple Assert Exception macro


Typedefs

typedef void Sigfunc (int)
 Replace the standard signal function with a more reliable using sigaction.

typedef myportPort_T
 Defines a port object.

typedef myprotocol * Protocol_T
 Defines a protocol object with protocol functions.

typedef mydependantDependant_T
typedef mychecksumChecksum_T
 Defines a checksum object.

typedef mymailMail_T
 Defines a mailinglist object.

typedef myresourceResource_T
 Defines resource data.

typedef mytimestampTimestamp_T
 Defines timestamp object.

typedef myprocinfoProcInfo_T
 Defines procfs (or other mechanism) data.

typedef mycommandCommand_T
 Defines a command with ARGMAX optional arguments.

typedef myprocessProcess_T
 Defines process data.


Functions

Sigfuncsignal (int signo, Sigfunc *func)
 Replace the standard signal() function, with a more reliable using sigaction.

void error (const char *,...)
 Print a formated message to stderr or to the logfile if no tty exist.

char * trim (char *)
 Remove leading and trailing space from the string.

char * ltrim (char *)
 Remove leading white space [ \t\r
] from the string.


char * rtrim (char *)
 Remove trailing white space [ \t\r
] from the string.


char * trim_quotes (char *)
 Remove any enclosing quotes ["'] from the string.

int starts_with (char *, char *)
 Return TRUE if the string a starts with the string b.

void handle_string_escapes (char *)
 Exchanges \escape sequences in a string.

void chomp (char *)
 Removes everything from the first newline (CR|LF).

int exist_process (char *)
Process_T get_process (char *)
void printrunlist ()
 Print the Runtime object.

void printprocess (Process_T)
 Print a process object.

void printprocesslist ()
 Print all the processes in the processlist.

pid_t get_pid (char *)
 Open and read the pid from the given pidfile.

int is_process_running (Process_T)
int is_strdefined (char *)
int isreg_file (char *)
 Check if the file is a regular file.

char * stripfilename (char *)
 Strip the path and return only the filename.

int exist_file (char *)
char * get_ctime ()
 Get a non
terminated ctime.


char * get_RFC1123date (long *)
 Returns a (RFC1123) Date string.

char * get_uptime (long delta)
char * get_process_uptime (char *)
 Compute an uptime string for a process based on the ctime from the pidfile.

char * get_md5sum (char *)
int check_md5 (char *, char *)
char * url_encode (char *uri)
 Escape an uri string converting unsafe characters to a hex (xx) representation.

char * get_basic_authentication_header ()
int set_md5sum (char **, char *)
 Compute a md5 checksum for the given file and save the result in an allocated area pointed to by dest.

int parse (char *)
void control (char *, int)
 Start/stop all processes in process list.

void control_group (char *, char *, int)
void d_check_process (char *, char *)
 Pass on to methods in http/cervlet.c to start/stop processes.

void check_process (char *, char *, int)
 Check to see if we should try to start/stop process.

void setup_dependants ()
void reset_depend ()
void spawn (Process_T, Command_T)
 Execute the given command.

void status ()
 Show all processes in the process list.

void status_group (char *)
 Show all processes in the group.

int log_init ()
 Initialize the log system and 'log' function.

void log (const char *format,...)
 Log a message to monits logfile or syslog.

void log_close ()
 Close the log file or syslog.

int get_log_fd ()
void validate ()
 Walk the process-list and validate each process.

void daemonize ()
 Transform a program into a daemon.

void gc ()
void gc_process (Process_T *)
void gc_mail_list (Mail_T *)
void init_files ()
 Initialize the programs file variables.

time_t get_timestamp (char *, mode_t)
 Get a object's last modified timestamp.

void finalize_files ()
 Remove temporary files.

char * find_rcfile ()
 Search the system for the monit control file.

int create_pidfile (char *)
 Create a program's pidfile - Such a file is created when in daemon mode.

int is_rcfile_changed ()
 Test the monit control file for changes.

int check_rcfile (char *)
 Secure check the monitrc file.

int kill_daemon ()
 Kill a daemon process.

int exist_daemon ()
void sendmail (Mail_T)
 Send SMTP mail messages.

int sock_msg (int sock, char *,...)
void init_env ()
 Initialize the program environment.

void * xmalloc (int)
void * xcalloc (long, long)
char * xstrdup (const char *)
void * xresize (void *, long)
void set_alarm_handler (void *)
 Set a handler for the alarm signal, SIGALRM using sigaction.

void monit_http (int)
 Start and stop the monit http server.

int check_httpd ()
 Check if the HTTPD server is running.

int can_http ()
char * format (const char *, va_list)
 Do printf style format line parsing.

void redirect_stdfd ()
 Redirect the standard file descriptors to /dev/null and route any error messages to the log file.

pid_t getpgid (pid_t)
int check_file (char *, char *, int)
 Security check for files.


Variables

Process_T processlist
 The process list (created in p.y).

char * prog
 The Name of this Program.

myrun Run
 Struct holding runtime constants.


Define Documentation

#define ACTION_ALERT   1
 

Definition at line 84 of file monitor.h.

#define ACTION_RESTART   2
 

Definition at line 85 of file monitor.h.

#define ACTION_STOP   3
 

Definition at line 86 of file monitor.h.

#define ARGMAX   64
 

Definition at line 46 of file monitor.h.

#define ASSERT  
 

Value:

if(!(e)) { error("AssertException: at %s:%d\naborting..\n", \
    __FILE__, __LINE__); abort(); }
------------------------------------------ Simple Assert Exception macro

Definition at line 112 of file monitor.h.

Referenced by accept_ssl_socket(), check_connect(), check_connection_io(), check_default(), check_file(), check_ftp(), check_host(), check_http(), check_imap(), check_md5(), check_nntp(), check_pop(), check_process(), check_rcfile(), check_smtp(), check_ssh(), check_ssl_md5sum(), chomp(), control(), control_group(), create_generic_socket(), create_pidfile(), create_socket(), create_ssl_server_socket(), create_ssl_socket(), create_unix_socket(), d_check_process(), embed_accepted_ssl_socket(), error(), exist_file(), exist_process(), format(), gc_mail_list(), gc_process(), get_md5sum(), get_pid(), get_process(), get_process_uptime(), get_timestamp(), gets_ssl_socket(), handle_string_escapes(), init_ssl_server(), insert_accepted_ssl_socket(), is_process_running(), isreg_file(), log(), ltrim(), new_ssl_server_connection(), port_recv(), port_send(), printf_ssl_socket(), printprocess(), read_proc_file(), recv_ssl_socket(), rtrim(), send_ssl_socket(), set_md5sum(), smtp_alert_checksum(), smtp_alert_failed(), smtp_alert_resource(), smtp_alert_restart(), smtp_alert_stop(), smtp_alert_timeout(), smtp_alert_timestamp(), sock_recv(), sock_send(), spawn(), status_group(), stripfilename(), trim(), trim_quotes(), update_process_data(), update_ssl_cert_data(), url_encode(), and xstrdup().

#define CHECK_TIMEOUT   30
 

Definition at line 58 of file monitor.h.

#define END_LOCK   pthread_mutex_unlock(_yymutex); } while (0)
 

Definition at line 103 of file monitor.h.

Referenced by add_host_allow(), delete_accepted_ssl_socket(), destroy_hosts_allow(), has_hosts_allow(), and insert_accepted_ssl_socket().

#define FALSE   0
 

Definition at line 65 of file monitor.h.

#define FORCE_CONTROL   0
 

Definition at line 67 of file monitor.h.

#define HTTP_CONTROL   1
 

Definition at line 68 of file monitor.h.

#define is a,
     ((a&&b)?!strcasecmp(a, b):0)
 

Definition at line 89 of file monitor.h.

Referenced by check_process(), control_group(), exist_process(), get_process(), and status_group().

#define LOCALHOST   "localhost"
 

Definition at line 54 of file monitor.h.

#define LOCK mutex   
 

Value:

do { pthread_mutex_t *_yymutex = &(mutex); \
        pthread_mutex_lock(_yymutex);
------------------------------------------------- Synchronization macros

Definition at line 101 of file monitor.h.

Referenced by add_host_allow(), delete_accepted_ssl_socket(), destroy_hosts_allow(), has_hosts_allow(), and insert_accepted_ssl_socket().

#define LOGMASK   0112
 

Definition at line 48 of file monitor.h.

#define MAXIMUM x,
     ((x) > (y) ? (x) : (y))
 

Definition at line 88 of file monitor.h.

Referenced by get_timestamp().

#define MODE_ACTIVE   0
 

Definition at line 70 of file monitor.h.

#define MODE_MANUAL   2
 

Definition at line 72 of file monitor.h.

#define MODE_PASSIVE   1
 

Definition at line 71 of file monitor.h.

#define MONITRC   "monitrc"
 

Definition at line 42 of file monitor.h.

Referenced by find_rcfile().

#define MYPIDDIR   PIDDIR
 

Definition at line 51 of file monitor.h.

Referenced by init_files().

#define MYPIDFILE   "monit.pid"
 

Definition at line 52 of file monitor.h.

Referenced by init_files().

#define NEW      (xcalloc(1, (long)sizeof *(p)))
 

----------------------------------------------- Object Constructor macro

Definition at line 108 of file monitor.h.

Referenced by add_host_allow(), create_default(), create_ftp(), create_http(), create_imap(), create_nntp(), create_pop(), create_smtp(), create_ssh(), insert_accepted_ssl_socket(), new_ssl_connection(), new_ssl_server_connection(), and set_header().

#define OPERATOR_EQUAL   2
 

Definition at line 76 of file monitor.h.

#define OPERATOR_GREATER   0
 

Definition at line 74 of file monitor.h.

#define OPERATOR_LESS   1
 

Definition at line 75 of file monitor.h.

#define OPERATOR_NOTEQUAL   3
 

Definition at line 77 of file monitor.h.

#define PIDMASK   0122
 

Definition at line 50 of file monitor.h.

Referenced by create_pidfile().

#define SMTP_PORT   25
 

Definition at line 55 of file monitor.h.

#define SMTP_TIMEOUT   30
 

Definition at line 57 of file monitor.h.

Referenced by sendmail().

#define SSL_TIMEOUT   5
 

Definition at line 59 of file monitor.h.

Referenced by embed_accepted_ssl_socket(), embed_ssl_socket(), recv_ssl_socket(), and send_ssl_socket().

#define START_HTTP   1
 

Definition at line 61 of file monitor.h.

Referenced by monit_http().

#define STOP_HTTP   2
 

Definition at line 62 of file monitor.h.

Referenced by monit_http().

#define STRERROR   strerror(errno)
 

Definition at line 44 of file monitor.h.

Referenced by check_file(), check_ftp(), check_http(), check_imap(), check_nntp(), check_pop(), check_smtp(), check_ssh(), create_pidfile(), daemonize(), get_pid(), kill_daemon(), log_close(), redirect_stdfd(), start_httpd(), xcalloc(), xmalloc(), and xresize().

#define STRLEN   256
 

Definition at line 45 of file monitor.h.

#define TIME_DAY   86400
 

Definition at line 82 of file monitor.h.

#define TIME_HOUR   3600
 

Definition at line 81 of file monitor.h.

#define TIME_MINUTE   60
 

Definition at line 80 of file monitor.h.

#define TIME_SECOND   1
 

Definition at line 79 of file monitor.h.

#define TIMEFORMAT   "%Z %b %e %T"
 

Definition at line 43 of file monitor.h.

#define TRUE   1
 

Definition at line 64 of file monitor.h.

#define VERSION   PACKAGE_VERSION
 

Definition at line 41 of file monitor.h.

Referenced by check_http(), and sendmail().


Typedef Documentation

typedef struct mychecksum * Checksum_T
 

Defines a checksum object.

typedef struct mycommand * Command_T
 

Defines a command with ARGMAX optional arguments.

The arguments array must be NULL terminated and the first entry is the program itself.

typedef struct mydependant * Dependant_T
 

typedef struct mymail * Mail_T
 

Defines a mailinglist object.

typedef struct myport * Port_T
 

Defines a port object.

typedef struct myprocess * Process_T
 

Defines process data.

typedef struct myprocinfo * ProcInfo_T
 

Defines procfs (or other mechanism) data.

typedef struct myprotocol* Protocol_T
 

Defines a protocol object with protocol functions.

Definition at line 195 of file monitor.h.

typedef struct myresource * Resource_T
 

Defines resource data.

typedef void Sigfunc(int)
 

Replace the standard signal function with a more reliable using sigaction.

Definition at line 93 of file monitor.h.

Referenced by signal().

typedef struct mytimestamp * Timestamp_T
 

Defines timestamp object.


Function Documentation

int can_http  
 

Returns:
TRUE if the monit http can start and is specified in the controlfile to start, otherwise return FALSE. Print an error message if monit httpd _should_ start but can't.

Definition at line 86 of file monit_http.c.

References myrun::Auth, myrun::myautentication::defined, myrun::dohttpd, has_hosts_allow(), myrun::isdaemon, log(), prog, and Run.

int check_file char *    filename,
char *    description,
int    permmask
 

Security check for files.

The files must have the same uid as the REAL uid of this process, it must have permissions no greater than "maxpermission" and it must not be a symbolic link. We check these conditions here.

Parameters:
filename  The filename of the checked file
description  The description of the checked file
permmask  The permission mask for the file
Returns:
TRUE if the test passed otherwise FALSE

Definition at line 284 of file files.c.

References ASSERT, log(), prog, and STRERROR.

Referenced by check_rcfile().

int check_httpd  
 

Check if the HTTPD server is running.

Definition at line 73 of file monit_http.c.

References myrun::bind_addr, check_connect(), myrun::httpdport, and Run.

int check_md5 char *    file,
char *    sum
 

Parameters:
file  A file to open and compute a md5 checksum for
sum  A previous checksum computed for this file
Returns:
TRUE if computation of md5sum on the given file, is equal to the given sum, otherwise return FALSE. FALSE is also returned if md5_stream fails.

Definition at line 918 of file util.c.

References ASSERT, and get_md5sum().

void check_process char *    P,
char *    action,
int    toggle_validate_flag
 

Check to see if we should try to start/stop process.

Parameters:
P  A process name as stated in the config file
action  A string describing the action to execute
toggle_validate_flag  passed on to the start/stop functions

Definition at line 195 of file control.c.

References ASSERT, myrun::controlfile, error(), get_process(), is, is_process_running(), prog, Run, myprocess::start, and myprocess::stop.

Referenced by control(), and control_group().

int check_rcfile char *    rcfile
 

Secure check the monitrc file.

The run control file must have the same uid as the REAL uid of this process, it must have permissions no greater than 700 and it must not be a symbolic link. We check these conditions here.

Parameters:
rcfile  The monitrc file
Returns:
TRUE if the test passed otherwise FALSE

Definition at line 233 of file files.c.

References ASSERT, and check_file().

void chomp char *    string
 

Removes everything from the first newline (CR|LF).

Parameters:
string  A string to be chomped

Definition at line 143 of file util.c.

References ASSERT.

Referenced by check_ftp(), check_http(), check_imap(), check_nntp(), check_pop(), check_smtp(), check_ssh(), and get_ctime().

void control char *    action,
int    toggle_validate_flag
 

Start/stop all processes in process list.

Parameters:
action  A string describing the action to execute
toggle_validate_flag  passed on to the start/stop functions

Definition at line 70 of file control.c.

References ASSERT, check_process(), d_check_process(), exist_daemon(), myprocess::name, myprocess::next, processlist, reset_depend(), and myprocess::visited.

void control_group char *   ,
char *   ,
int   
 

Definition at line 97 of file control.c.

References ASSERT, check_process(), d_check_process(), exist_daemon(), myprocess::group, is, myprocess::name, myprocess::next, processlist, reset_depend(), and myprocess::visited.

int create_pidfile char *    pidfile
 

Create a program's pidfile - Such a file is created when in daemon mode.

The file is created with mask = PIDMASK (usually 644).

Parameters:
pidfile  The name of the pidfile to create
Returns:
TRUE if the file was created, otherwise FALSE.

Definition at line 188 of file files.c.

References ASSERT, log(), PIDMASK, prog, and STRERROR.

void d_check_process char *    P,
char *    action
 

Pass on to methods in http/cervlet.c to start/stop processes.

Parameters:
P  A process name as stated in the config file
action  A string describing the action to execute

Definition at line 126 of file control.c.

References ASSERT, myrun::bind_addr, close_socket(), close_ssl_socket(), create_socket(), delete_ssl_socket(), embed_ssl_socket(), error(), get_basic_authentication_header(), myrun::httpdport, myrun::httpdssl, myrun::httpsslpem, new_ssl_connection(), prog, Run, send_ssl_socket(), sock_send(), and ssl_connection.

Referenced by control(), and control_group().

void daemonize  
 

Transform a program into a daemon.

Inspired by code from Stephen A. Rago's book, Unix System V Network Programming.

Definition at line 70 of file daemonize.c.

References log(), redirect_stdfd(), signal(), and STRERROR.

void error const char *    format,
...   
 

Print a formated message to stderr or to the logfile if no tty exist.

Parameters:
format  A formate (printf-style) string

Definition at line 86 of file util.c.

References ASSERT, format(), myrun::have_tty, log(), and Run.

Referenced by check_process(), create_ssl_server_socket(), create_ssl_socket(), d_check_process(), embed_accepted_ssl_socket(), embed_ssl_socket(), init_ssl_server(), kill_daemon(), log_close(), new_ssl_connection(), recv_ssl_socket(), redirect_stdfd(), send_ssl_socket(), start_httpd(), xcalloc(), xmalloc(), and xresize().

int exist_daemon  
 

Returns:
TRUE (i.e. the daemons pid) if a daemon process is running, otherwise FALSE

Definition at line 177 of file daemonize.c.

References is_process_running(), myrun::pidfile, myprocess::pidfile, and Run.

Referenced by control(), control_group(), and kill_daemon().

int exist_file char *   
 

Definition at line 263 of file files.c.

References ASSERT.

Referenced by find_rcfile(), and get_pid().

int exist_process char *    name
 

Parameters:
name  A process name as stated in the config file
Returns:
TRUE if the process name exist in the processlist, otherwise FALSE

Definition at line 343 of file util.c.

References ASSERT, is, myprocess::name, myprocess::next, and processlist.

void finalize_files  
 

Remove temporary files.

Definition at line 98 of file files.c.

References myrun::pidfile, and Run.

char* find_rcfile  
 

Search the system for the monit control file.

Try first ~/.monitrc, if that fails try ./monitrc and finally /etc/monitrc. Exit the application if the control file is not found.

Returns:
The location of monits control file (monitrc)

Definition at line 147 of file files.c.

References myrun::myenvironment::cwd, myrun::Env, exist_file(), myrun::myenvironment::home, log(), MONITRC, prog, Run, and xmalloc().

char* format const char *    s,
va_list    ap
 

Do printf style format line parsing.

Parameters:
s  format string
ap  variable argument list
Returns:
buffer with parsed string

Definition at line 1009 of file util.c.

References ASSERT, xmalloc(), and xresize().

Referenced by error().

void gc  
 

Definition at line 55 of file gc.c.

References gc_protocols(), myrun::mygroup, processlist, and Run.

void gc_mail_list Mail_T  
 

Definition at line 131 of file gc.c.

References ASSERT.

Referenced by gc_process(), and spawn().

void gc_process Process_T  
 

Definition at line 64 of file gc.c.

References ASSERT, and gc_mail_list().

char* get_basic_authentication_header  
 

Returns:
a Basic Authentication Authorization string (RFC 2617), with credentials from the Run object, The string "\r
" if credentials are not defined.

Definition at line 981 of file util.c.

References myrun::Auth, myrun::myautentication::defined, encode_base64(), myrun::myautentication::passwd, Run, myrun::myautentication::uname, xmalloc(), and xstrdup().

Referenced by d_check_process().

char* get_ctime  
 

Get a non
terminated ctime.

Returns:
a ctime string (i.e. date)

Definition at line 768 of file util.c.

References chomp(), and xmalloc().

int get_log_fd  
 

Returns:
the log filedescriptor or -1 if it's not open, i.e. syslog is used or logging is not activated

Definition at line 173 of file log.c.

char* get_md5sum char *    file
 

Returns:
a md5 checksum for the given file, or NULL if error.

Definition at line 869 of file util.c.

References ASSERT, isreg_file(), and xstrdup().

Referenced by check_md5(), and set_md5sum().

pid_t get_pid char *    pidfile
 

Open and read the pid from the given pidfile.

Parameters:
pidfile  A pidfile with full path
Returns:
the pid (TRUE) or FALSE if the pid could not be read from the file

Definition at line 649 of file util.c.

References ASSERT, exist_file(), isreg_file(), log(), prog, and STRERROR.

Referenced by is_process_running().

Process_T get_process char *    name
 

Parameters:
name  A process name as stated in the config file
Returns:
the named process or NULL if not found

Definition at line 321 of file util.c.

References ASSERT, is, myprocess::name, myprocess::next, and processlist.

Referenced by check_process().

char* get_process_uptime char *    pidfile
 

Compute an uptime string for a process based on the ctime from the pidfile.

The caller must free the returned string.

Parameters:
pidfile  A process pidfile
Returns:
an uptime string

Definition at line 788 of file util.c.

References ASSERT, get_timestamp(), get_uptime(), and xstrdup().

Referenced by status().

char* get_RFC1123date long *    date
 

Returns a (RFC1123) Date string.

If the given date is NULL compute the date now.

Parameters:
date 
Returns:
a date string alligned with RFC1123

Definition at line 726 of file util.c.

References xstrdup().

Referenced by sendmail().

time_t get_timestamp char *    object,
mode_t    type
 

Get a object's last modified timestamp.

Parameters:
object  A object to stat
type  Requested object's type
Returns:
Max of either st_mtime or st_ctime or FALSE if not found or different type of object

Definition at line 112 of file files.c.

References ASSERT, log(), MAXIMUM, and prog.

Referenced by get_process_uptime(), init_files(), and is_rcfile_changed().

char* get_uptime long    delta
 

pid_t getpgid pid_t   
 

Referenced by is_process_running().

void handle_string_escapes char *    buf
 

Exchanges \escape sequences in a string.

Parameters:
buf  A string

Definition at line 262 of file util.c.

References ASSERT.

void init_env  
 

Initialize the program environment.

Definition at line 73 of file env.c.

References myrun::have_tty, and Run.

Referenced by main().

void init_files  
 

Initialize the programs file variables.

Definition at line 68 of file files.c.

References myrun::controlfile, myrun::Env, get_timestamp(), myrun::myenvironment::home, MYPIDDIR, MYPIDFILE, myrun::pidfile, Run, myrun::timestamp, and xstrdup().

int is_process_running Process_T    p
 

Returns:
TRUE (i.e. the running pid id) if the process is running, otherwise FALSE

Definition at line 698 of file util.c.

References ASSERT, get_pid(), getpgid(), and myprocess::pidfile.

Referenced by check_process(), and exist_daemon().

int is_rcfile_changed  
 

Test the monit control file for changes.

Returns:
TRUE if the Runtime control file was changed, otherwise FALSE

Definition at line 218 of file files.c.

References myrun::controlfile, get_timestamp(), Run, and myrun::timestamp.

int is_strdefined char *    p
 

Returns:
TRUE if the string parameter is defined, otherwise FALSE

Definition at line 114 of file util.c.

int isreg_file char *    file
 

Check if the file is a regular file.

Parameters:
file  A path to the file to check
Returns:
TRUE if file exist and is a regular file, otherwise FALSE

Definition at line 247 of file files.c.

References ASSERT.

Referenced by get_md5sum(), and get_pid().

int kill_daemon  
 

Kill a daemon process.

Returns:
TRUE if the daemon was killed, otherwise FALSE

Definition at line 145 of file daemonize.c.

References error(), exist_daemon(), prog, and STRERROR.

void log const char *    format,
...   
 

Log a message to monits logfile or syslog.

Parameters:
format  A formated (printf-style) string to log

Definition at line 111 of file log.c.

References ASSERT, myrun::dolog, myrun::have_tty, Run, and myrun::use_syslog.

Referenced by can_http(), check_file(), check_ftp(), check_http(), check_imap(), check_nntp(), check_pop(), check_smtp(), check_ssh(), create_pidfile(), daemonize(), error(), find_rcfile(), get_pid(), get_timestamp(), monit_http(), out_print(), spawn(), and validate().

void log_close  
 

Close the log file or syslog.

Definition at line 149 of file log.c.

References error(), prog, Run, STRERROR, and myrun::use_syslog.

Referenced by log_init().

int log_init  
 

Initialize the log system and 'log' function.

Returns:
TRUE if the log system was successfully initialized

Definition at line 84 of file log.c.

References myrun::dolog, log_close(), and Run.

char* ltrim char *    s
 

Remove leading white space [ \t\r
] from the string.

Parameters:
s  A string
Returns:
s with leading spaces removed

Definition at line 176 of file util.c.

References ASSERT.

Referenced by trim().

void monit_http int    action
 

Start and stop the monit http server.

Parameters:
action  START_HTTP or STOP_HTTP

Definition at line 112 of file monit_http.c.

References log(), prog, START_HTTP, STOP_HTTP, and stop_httpd().

int parse char *   
 

void printprocess Process_T    p
 

Print a process object.

Parameters:
p  A Process_T object

Definition at line 426 of file util.c.

References mytimestamp::action, myresource::action, mymail::alert_on_checksum, mymail::alert_on_resource, mymail::alert_on_restart, mymail::alert_on_stop, mymail::alert_on_timeout, mymail::alert_on_timestamp, mycommand::arg, ASSERT, myport::certmd5, myprocess::checksumlist, myprocess::def_every, myprocess::def_timeout, mydependant::dependant, myprocess::dependantlist, myprocess::every, myport::family, mychecksum::file, mymail::from, myprocess::group, myport::hostname, myresource::limit, myprocess::maillist, myresource::max_cycle, mychecksum::md5, mymail::message, myprocess::mode, myprocess::name, myport::myprotocol::name, mychecksum::next, mydependant::next, myport::next, mytimestamp::next, myresource::next, mymail::next, mytimestamp::operator, myresource::operator, myport::pathname, mytimestamp::pathname, myprocess::pidfile, myport::port, myprocess::portlist, myport::protocol, myport::request, myresource::resource_id, RESOURCE_ID_CPU_PERCENT, RESOURCE_ID_LOAD1, RESOURCE_ID_LOAD15, RESOURCE_ID_LOAD5, RESOURCE_ID_MEM_KBYTE, RESOURCE_ID_MEM_PERCENT, myprocess::resourcelist, myport::ssl, myprocess::start, myprocess::stop, mymail::subject, mytimestamp::time, myprocess::timestamplist, mymail::to, myprocess::to_cycle, and myprocess::to_start.

Referenced by printprocesslist().

void printprocesslist  
 

Print all the processes in the processlist.

Definition at line 624 of file util.c.

References myprocess::next, printprocess(), and processlist.

void printrunlist  
 

Print the Runtime object.

Definition at line 361 of file util.c.

References myrun::allowselfcert, myrun::Auth, myrun::bind_addr, myrun::controlfile, myrun::debug, myrun::myautentication::defined, myrun::dohttpd, myrun::dolog, myrun::doprocess, myrun::myformat::from, myrun::httpdport, myrun::httpdssl, myrun::httpsslclientpem, myrun::httpsslpem, myrun::isdaemon, myrun::logfile, myrun::MailFormat, myrun::mailserver, myrun::myformat::message, myrun::pidfile, myrun::polltime, Run, myrun::myformat::subject, and myrun::use_syslog.

void redirect_stdfd  
 

Redirect the standard file descriptors to /dev/null and route any error messages to the log file.

Definition at line 1042 of file util.c.

References error(), myrun::have_tty, Run, and STRERROR.

Referenced by daemonize(), and spawn().

void reset_depend  
 

Definition at line 243 of file control.c.

References myprocess::depend_visited, myprocess::next, processlist, and myprocess::visited.

Referenced by control(), control_group(), and validate().

char* rtrim char *    s
 

Remove trailing white space [ \t\r
] from the string.

Parameters:
s  A string
Returns:
s with trailing spaces removed

Definition at line 194 of file util.c.

References ASSERT.

Referenced by trim().

void sendmail Mail_T    mail
 

Send SMTP mail messages.

Parameters:
mail  A mymail struct object

Definition at line 78 of file sendmail.c.

References mymail::from, get_localhostname(), get_RFC1123date(), mymail::message, mymail::next, mymail::opt_message, prog, SMTP_TIMEOUT, mymail::subject, mymail::to, and VERSION.

Referenced by spawn().

void set_alarm_handler void *    func
 

Set a handler for the alarm signal, SIGALRM using sigaction.

Definition at line 75 of file signal.c.

int set_md5sum char **    dest,
char *    file
 

Compute a md5 checksum for the given file and save the result in an allocated area pointed to by dest.

The caller is responsible for freeing dest.

Parameters:
dest  a pointer to an allocated area with the computed md5 sum
file  The name of file to compute a md5 sum for
Returns:
TRUE if success otherwise FALSE.

Definition at line 853 of file util.c.

References ASSERT, and get_md5sum().

void setup_dependants  
 

Sigfunc* signal int    signo,
Sigfunc   func
 

Replace the standard signal() function, with a more reliable using sigaction.

From W. Richard Stevens' "Advanced Programming in the UNIX Environment"

Definition at line 47 of file signal.c.

References Sigfunc.

Referenced by daemonize(), and spawn().

int sock_msg int    sock,
char *   ,
...   
 

void spawn Process_T    P,
Command_T    C
 

Execute the given command.

If the execution fails, an alert message is sent to the email addresses found in the process object that has registred interest for restart alerts.

Parameters:
P  A Process object
C  A Command object

Definition at line 84 of file spawn.c.

References mycommand::arg, ASSERT, gc_mail_list(), log(), redirect_stdfd(), Run, sendmail(), signal(), and myrun::umask.

int starts_with char *    a,
char *    b
 

Return TRUE if the string a starts with the string b.

Parameters:
a  The string to search for b in
b  The sub-string to test a against
Returns:
TRUE if a starts with b, otherwise FALSE

Definition at line 242 of file util.c.

void status  
 

Show all processes in the process list.

Definition at line 69 of file status.c.

References delete_ssl_socket(), get_process_uptime(), myrun::httpdssl, myrun::httpsslpem, new_ssl_connection(), myprocess::next, myrun::pidfile, processlist, Run, and ssl_connection.

void status_group char *    G
 

Show all processes in the group.

Parameters:
G  group name

Definition at line 111 of file status.c.

References ASSERT, delete_ssl_socket(), myprocess::group, myrun::httpdssl, myrun::httpsslclientpem, is, new_ssl_connection(), myprocess::next, processlist, Run, and ssl_connection.

char* stripfilename char *    path
 

Strip the path and return only the filename.

Parameters:
path  A file path string
Returns:
the basename

Definition at line 126 of file util.c.

References ASSERT.

Referenced by main().

char* trim char *    s
 

Remove leading and trailing space from the string.

Parameters:
s  A string
Returns:
s with leading and trailing spaces removed

Definition at line 159 of file util.c.

References ASSERT, ltrim(), and rtrim().

char* trim_quotes char *    s
 

Remove any enclosing quotes ["'] from the string.

Parameters:
s  A string
Returns:
s with any enclosed quotes removed

Definition at line 214 of file util.c.

References ASSERT.

char* url_encode char *    uri
 

Escape an uri string converting unsafe characters to a hex (xx) representation.

The caller must free the returned string.

Parameters:
uri  an uri string
Returns:
the escaped string

Definition at line 949 of file util.c.

References ASSERT, and xmalloc().

void validate  
 

Walk the process-list and validate each process.

Definition at line 90 of file validate.c.

References log(), myprocess::next, processlist, reset_depend(), update_loadavg(), and myprocess::visited.

void* xcalloc long   ,
long   
 

Definition at line 81 of file xmalloc.c.

References error(), prog, and STRERROR.

void* xmalloc int   
 

Definition at line 52 of file xmalloc.c.

References error(), prog, and STRERROR.

Referenced by decode_base64(), encode_base64(), find_rcfile(), format(), get_basic_authentication_header(), get_ctime(), makeword(), out_print(), url_encode(), xresize(), and xstrdup().

void* xresize void *   ,
long   
 

Definition at line 112 of file xmalloc.c.

References error(), prog, STRERROR, and xmalloc().

Referenced by format(), and out_print().

char* xstrdup const char *   
 

Definition at line 98 of file xmalloc.c.

References ASSERT, and xmalloc().

Referenced by add_host_allow(), get_basic_authentication_header(), get_header(), get_headers(), get_md5sum(), get_parameter(), get_process_uptime(), get_RFC1123date(), get_status_string(), get_uptime(), init_files(), insert_accepted_ssl_socket(), new_ssl_connection(), new_ssl_server_connection(), set_header(), set_status(), and update_ssl_cert_data().


Variable Documentation

Process_T processlist
 

The process list (created in p.y).

Definition at line 314 of file monitor.h.

Referenced by control(), control_group(), exist_process(), gc(), get_process(), printprocesslist(), reset_depend(), status(), status_group(), and validate().

char* prog
 

The Name of this Program.

Definition at line 315 of file monitor.h.

Referenced by can_http(), check_file(), check_http(), check_process(), create_pidfile(), create_ssl_server_socket(), create_ssl_socket(), d_check_process(), embed_accepted_ssl_socket(), embed_ssl_socket(), find_rcfile(), get_pid(), get_timestamp(), init_ssl_server(), kill_daemon(), log_close(), main(), monit_http(), new_ssl_connection(), recv_ssl_socket(), send_ssl_socket(), sendmail(), xcalloc(), xmalloc(), and xresize().

struct myrun Run
 

Struct holding runtime constants.

Definition at line 316 of file monitor.h.

Referenced by can_http(), check_httpd(), check_process(), d_check_process(), destroy_wrapper(), error(), exist_daemon(), finalize_files(), find_rcfile(), gc(), get_basic_authentication_header(), init_env(), init_files(), is_rcfile_changed(), log(), log_close(), log_init(), printrunlist(), redirect_stdfd(), spawn(), start_httpd(), status(), status_group(), and update_loadavg().