status.c

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 #include <config.h>
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #ifdef HAVE_SYS_TYPES_H
00025 #include <sys/types.h>
00026 #endif
00027 #include <sys/socket.h>
00028 
00029 #ifdef HAVE_STRING_H
00030 #include <string.h>
00031 #endif
00032 
00033 #ifdef HAVE_STRINGS_H
00034 #include <strings.h>
00035 #endif
00036 
00037 #ifdef HAVE_UNISTD_H
00038 #include <unistd.h>
00039 #endif
00040 
00041 #include "net.h"
00042 #include "monitor.h"
00043 #include "monit_process.h"
00044 
00045 
00046 /* Private Prototypes */
00047 static void local_status(Process_T);
00048 static int remote_status(Process_T);
00049 
00060 /* ------------------------------------------------------------------ Public */
00061 
00062 
00066 void status() {
00067 
00068   Process_T p;
00069   char *uptime= get_process_uptime(Run.pidfile);
00070   int remote= TRUE;
00071   
00072   fprintf(stdout, "monit daemon uptime: %s\n", uptime);
00073   free(uptime);
00074   
00075   for ( p= processlist; p; p= p->next) {
00076     
00077       if (remote) {
00078 
00079     remote = remote_status(p);
00080 
00081       } else {
00082 
00083     local_status(p);
00084 
00085       }
00086     
00087   }
00088   
00089 }
00090 
00095 void status_group(char *G) {
00096 
00097   Process_T p;
00098   int remote= TRUE;
00099 
00100   for ( p= processlist; p; p= p->next) {
00101 
00102     if (is(p->group, G) ) {
00103 
00104       if (remote) {
00105 
00106     remote = remote_status(p);
00107 
00108       } else {
00109 
00110     local_status(p);
00111 
00112       }
00113 
00114     }
00115    
00116   }
00117 
00118 }
00119 
00120 /* ----------------------------------------------------------------- Private */
00121 
00122 
00127 static void local_status(Process_T p) {
00128   
00129   pid_t  pid= -1;
00130   
00131   if ( (pid= is_process_running(p)) ) {
00132 
00133     char *uptime= get_process_uptime(p->pidfile);
00134 
00135     fprintf(stdout, "Process '%s' is running with pid [%d] Uptime: %s\n", 
00136         p->name, (int)pid, uptime);
00137     
00138     free(uptime);
00139     
00140   } else {
00141     
00142     fprintf(stdout, "Process '%s' is not running\n",  p->name);
00143     
00144   }
00145   
00146 }
00147 
00148 
00154 static int remote_status(Process_T p) {
00155 
00156   if ( exist_daemon() ) {
00157     
00158     /* If a monit daemon exist we request status information from the server */
00159     
00160     int s= create_socket(Run.bind_addr?Run.bind_addr:"localhost",
00161              Run.httpdport, SOCK_STREAM);
00162     if (s<0) {
00163 
00164       fprintf(stdout,
00165           "Cannot connect to monit server to get extended process data.\n");
00166       local_status(p);
00167 
00168       return FALSE;
00169             
00170     } else {
00171 
00172       int n;
00173       char req[2*STRLEN];
00174       char *auth= get_basic_authentication_header();
00175       char buf[STRLEN];
00176 
00177       snprintf(req, 2*STRLEN,
00178            "GET /%s?action=status HTTP/1.0\r\n%s\r\n", p->name, auth);
00179       
00180       free(auth);
00181       sock_send(s, req, sizeof(req), 0);
00182       
00183       if(0>(n= sock_recv(s, buf, STRLEN, 0))) {
00184     
00185     local_status(p);
00186     close_socket(s);
00187     
00188     return TRUE;
00189     
00190       }
00191       
00192       close_socket(s);
00193 
00194       /* If everything has gone well the returned string starts with
00195      "Process " */
00196 
00197       buf[n]= 0;
00198       if(starts_with(buf, "Process ")) {
00199 
00200     fprintf(stdout, "%s", buf);
00201     
00202       } else {
00203 
00204     fprintf(stdout, "The monit server did not return a process record.\n");
00205     local_status(p);
00206     
00207     return TRUE;
00208 
00209       }
00210 
00211       return TRUE;
00212     }
00213     
00214   } else {
00215     
00216     /* No monit daemon exist, just print local status information */
00217     
00218     fprintf(stdout, "Cannot connect to the monit server to get extended "
00219         "process data.\n");
00220     local_status(p);
00221     
00222     return FALSE;
00223   }
00224 
00225 }