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   snprintf(filename, STRLEN, "/proc/%d/%s", pid, name);
00083 
00084   fd = open(filename, O_RDONLY);
00085 
00086   if ( fd < 0 ) {
00087 
00088     return FALSE;
00089 
00090   }
00091 
00092   if ( (bytes = read(fd, buf, buf_size-1)) < 0 ) {
00093 
00094     return FALSE;
00095 
00096   }
00097        
00098   /* In case it is a string we have to 0 terminate it our self */
00099   buf[bytes]='\0';
00100 
00101   close(fd);
00102 
00103   return TRUE;
00104   
00105 }
00106 
00111 double get_float_time(void) {    
00112 
00113   struct timeval t;
00114 
00115   gettimeofday(&t, NULL);
00116   return (double) t.tv_sec * 10 + (double) t.tv_usec / 100000.0;
00117 
00118 }
00119 
00126 int get_process_info(ProcInfo_T p) {
00127   double temp_time;
00128 
00129   /* get the actual time */
00130 
00131   temp_time = get_float_time();
00132   
00133   /* get the system dependant proc data */
00134 
00135   if ( ! get_process_info_sysdep(p) ) {
00136 
00137     return FALSE;
00138 
00139   }
00140 
00141   /* check if it is the first run */
00142   
00143   if ( p->time_prev == 0.0 ) {
00144     
00145     p->cpu_percent= 0;
00146     
00147   } else {
00148     
00149     /* Calculate CPU percentage */
00150 
00151     p->cpu_percent=  (int) (( p->cputime - p->cputime_prev ) * 1000) / 
00152       ( temp_time - p->time_prev );
00153     
00154   }
00155 
00156   p->time_prev = temp_time;
00157     
00158 
00159   return TRUE;
00160 
00161 }
00162