common.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 
00031 #include <config.h>
00032 
00033 #ifdef HAVE_SYS_TYPES_H
00034 #include <sys/types.h>
00035 #endif
00036 
00037 #ifdef HAVE_SYS_STAT_H
00038 #include <sys/stat.h>
00039 #endif
00040 
00041 #ifdef HAVE_STRING_H
00042 #include <string.h>
00043 #endif
00044 
00045 #ifdef HAVE_SYS_TIME_H
00046 #include <sys/time.h>
00047 #endif
00048 
00049 #ifdef HAVE_LOADAVG_H
00050 #include <sys/loadavg.h>
00051 #endif
00052 
00053 #ifdef HAVE_STRING_H
00054 #include <string.h>
00055 #endif
00056 
00057 #ifdef HAVE_FCNTL_H
00058 #include <fcntl.h>
00059 #endif
00060 
00061 #include <stdio.h>
00062 #include <errno.h>
00063 
00064 
00065 #include "process.h"
00066 
00067 
00076 int read_proc_file(char *buf, int buf_size, char * name, int pid) {
00077 
00078   int fd;
00079   char filename[STRLEN];
00080   int bytes;
00081 
00082   ASSERT(buf);
00083   ASSERT(name);
00084 
00085   snprintf(filename, STRLEN, "/proc/%d/%s", pid, name);
00086 
00087   fd = open(filename, O_RDONLY);
00088 
00089   if ( fd < 0 ) {
00090 
00091     return FALSE;
00092 
00093   }
00094 
00095   if ( (bytes = read(fd, buf, buf_size-1)) < 0 ) {
00096 
00097     return FALSE;
00098 
00099   }
00100        
00101   /* In case it is a string we have to 0 terminate it our self */
00102   buf[bytes]='\0';
00103 
00104   close(fd);
00105 
00106   return TRUE;
00107   
00108 }
00109 
00114 double get_float_time(void) {    
00115 
00116   struct timeval t;
00117 
00118   gettimeofday(&t, NULL);
00119   return (double) t.tv_sec * 10 + (double) t.tv_usec / 100000.0;
00120 
00121 }
00122 
00129 int get_process_info(ProcInfo_T p) {
00130   double temp_time;
00131 
00132   /* get the actual time */
00133 
00134   temp_time = get_float_time();
00135   
00136   /* get the system dependant proc data */
00137 
00138   if ( ! get_process_info_sysdep(p) ) {
00139 
00140     return FALSE;
00141 
00142   }
00143 
00144   /* check if it is the first run */
00145   
00146   if ( p->time_prev == 0.0 ) {
00147     
00148     p->cpu_percent= 0;
00149     
00150   } else {
00151     
00152     /* Calculate CPU percentage */
00153 
00154     p->cpu_percent=  (int) (( p->cputime - p->cputime_prev ) * 1000) / 
00155       ( temp_time - p->time_prev ) / num_cpus;
00156     
00157   }
00158 
00159   p->time_prev = temp_time;
00160     
00161 
00162   return TRUE;
00163 
00164 }
00165