File: logfile.c

package info (click to toggle)
procmeter3 3.6-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,444 kB
  • sloc: ansic: 16,600; makefile: 428; sh: 13
file content (328 lines) | stat: -rw-r--r-- 8,663 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/***************************************
  $Header: /home/amb/CVS/procmeter3/modules/logfile.c,v 1.9 2008-05-05 18:45:35 amb Exp $

  ProcMeter - A system monitoring program for Linux - Version 3.5b.

  A log file monitoring source file.
  ******************/ /******************
  Written by Andrew M. Bishop

  This file Copyright 1998-2008 Andrew M. Bishop
  It may be distributed under the GNU Public License, version 2, or
  any higher version.  See section COPYING of the GNU Public license
  for conditions under which this file may be redistributed.
  ***************************************/


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/stat.h>
#include <sys/types.h>

#include "procmeter.h"

/* The interface information.  */

/*+ The template for the log file +*/
ProcMeterOutput _outputs[4]=
{
 /*+ The size in bytes +*/
 {
  /* char  name[];          */ "Log_Size_%s",
  /* char *description;     */ "The size of the log file '%s' in KBytes.",
  /* char  type;            */ PROCMETER_GRAPH|PROCMETER_TEXT|PROCMETER_BAR,
  /* short interval;        */ 1,
  /* char  text_value[];    */ "0 KB",
  /* long  graph_value;     */ 0,
  /* short graph_scale;     */ 100,
  /* char  graph_units[];   */ "(%dKB)"
 },
 /*+ The rate of change of size in bytes +*/
 {
  /* char  name[];          */ "Log_Grow_%s",
  /* char *description;     */ "The rate at which the size of the log file '%s' is increasing in KBytes/second.",
  /* char  type;            */ PROCMETER_GRAPH|PROCMETER_TEXT|PROCMETER_BAR,
  /* short interval;        */ 1,
  /* char  text_value[];    */ "0 KB/s",
  /* long  graph_value;     */ 0,
  /* short graph_scale;     */ 1,
  /* char  graph_units[];   */ "(%dK/s)"
 },
 /*+ The number of lines +*/
 {
  /* char  name[];          */ "Log_Line_%s",
  /* char *description;     */ "The number of lines in the log file '%s'.",
  /* char  type;            */ PROCMETER_GRAPH|PROCMETER_TEXT|PROCMETER_BAR,
  /* short interval;        */ 1,
  /* char  text_value[];    */ "0 lines",
  /* long  graph_value;     */ 0,
  /* short graph_scale;     */ 100,
  /* char  graph_units[];   */ "(%d)"
 },
 /*+ The rate of change of number of lines +*/
 {
  /* char  name[];          */ "Log_Rate_%s",
  /* char *description;     */ "The number of lines by which the log file '%s' is increasing per second.",
  /* char  type;            */ PROCMETER_GRAPH|PROCMETER_TEXT|PROCMETER_BAR,
  /* short interval;        */ 1,
  /* char  text_value[];    */ "0 lines/s",
  /* long  graph_value;     */ 0,
  /* short graph_scale;     */ 10,
  /* char  graph_units[];   */ "(%d/s)"
 }
};


/*+ The outputs. +*/
ProcMeterOutput **outputs=NULL;

/*+ The module. +*/
ProcMeterModule module=
{
 /* char name[];            */ "LogFile",
 /* char *description;      */ "The size and number of lines in the specified log files.  "
                               "(Use 'options=<filename1> <filename2>' in the configuration file to specify the files.)"
};


/* The files to monitor and the information about them */
static int nfiles=0;
static char **file=NULL;
static long *last=NULL;
static long *mtime=NULL;
static long *size=NULL;
static long *grow=NULL;
static long *line=NULL;
static long *rate=NULL;

/* Add a file to be monitored */
static void add_file(char *fil);


/*++++++++++++++++++++++++++++++++++++++
  Load the module.

  ProcMeterModule *Load Returns the module information.
  ++++++++++++++++++++++++++++++++++++++*/

ProcMeterModule *Load(void)
{
 return(&module);
}


/*++++++++++++++++++++++++++++++++++++++
  Initialise the module, creating the outputs as required.

  ProcMeterOutput **Initialise Returns a NULL terminated list of outputs.

  char *options The options string for the module from the .procmeterrc file.
  ++++++++++++++++++++++++++++++++++++++*/

ProcMeterOutput **Initialise(char *options)
{
 outputs=(ProcMeterOutput**)malloc(sizeof(ProcMeterOutput*));
 outputs[0]=NULL;

 /* Get the options */

 if(options)
   {
    char *l=options;

    while(*l && *l==' ')
       l++;

    while(*l)
      {
       char *r=l,pr;

       while(*r && *r!=' ')
          r++;

       pr=*r;
       *r=0;

       add_file(l);

       *r=pr;
       while(*r && *r==' ')
          r++;

       if(!*r)
          break;

       l=r;
      }
   }

 return(outputs);
}


/*++++++++++++++++++++++++++++++++++++++
  Add a new file to the list.

  char *fil The name of the file to add.
  ++++++++++++++++++++++++++++++++++++++*/

static void add_file(char *fil)
{
 int i,j;
 char *slash,*lastslash=fil-1;

 for(i=0;i<nfiles;i++)
    if(!strcmp(file[i],fil))
       return;

 while((slash=strchr(lastslash+1,'/')))
    lastslash=slash;

 outputs=(ProcMeterOutput**)realloc((void*)outputs,(nfiles*4+5)*sizeof(ProcMeterOutput*));

 for(i=0,j=4*nfiles;i<4;i++,j++)
   {
    outputs[j]=(ProcMeterOutput*)malloc(sizeof(ProcMeterOutput));

    *outputs[j]=_outputs[i];
    snprintf(outputs[j]->name, PROCMETER_NAME_LEN+1, _outputs[i].name, lastslash+1);
    outputs[j]->description=(char*)malloc(strlen(fil)+strlen(_outputs[i].description)+4);
    sprintf(outputs[j]->description,_outputs[i].description,fil);
   }

 file =(char**)realloc((void*)file ,(nfiles+1)*sizeof(char*));
 last =(long *)realloc((void*)last ,(nfiles+1)*sizeof(long));
 mtime=(long *)realloc((void*)mtime,(nfiles+1)*sizeof(long));
 size =(long *)realloc((void*)size ,(nfiles+1)*sizeof(long));
 grow =(long *)realloc((void*)grow ,(nfiles+1)*sizeof(long));
 line =(long *)realloc((void*)line ,(nfiles+1)*sizeof(long));
 rate =(long *)realloc((void*)rate ,(nfiles+1)*sizeof(long));

 file[nfiles]=(char*)malloc(strlen(fil)+1);
 strcpy(file[nfiles],fil);
 last[nfiles]=mtime[nfiles]=size[nfiles]=grow[nfiles]=line[nfiles]=rate[nfiles]=0;
 nfiles++;

 outputs[j]=NULL;
}


/*++++++++++++++++++++++++++++++++++++++
  Perform an update on one of the statistics.

  int Update Returns 0 if OK, else -1.

  time_t now The current time.

  ProcMeterOutput *output The output that the value is wanted for.
  ++++++++++++++++++++++++++++++++++++++*/

int Update(time_t now,ProcMeterOutput *output)
{
 int i;

 for(i=0;outputs[i];i++)
    if(output==outputs[i])
      {
       if(last[i/4]!=now)
         {
          struct stat buf;

          if(stat(file[i/4],&buf))
             mtime[i/4]=size[i/4]=grow[i/4]=line[i/4]=rate[i/4]=0;
          else
            {
             int lines=0;

             if(buf.st_size<size[i/4])
                size[i/4]=line[i/4]=0;

             if(buf.st_size>size[i/4])
               {
                FILE *f=fopen(file[i/4],"r");
                char buffer[2048];
                int nread,byte;

                if(f)
                  {
                   fseek(f,size[i/4],SEEK_SET);

                   while((nread=fread(buffer,1,2048,f))>0)
                      for(byte=0;byte<nread;byte++)
                         if(buffer[byte]=='\n')
                            lines++;

                   fclose(f);
                  }
               }

             mtime[i/4]=buf.st_mtime;
             grow[i/4]=(buf.st_size-size[i/4])/(now-last[i/4]);
             size[i/4]=buf.st_size;
             rate[i/4]=lines/(now-last[i/4]);
             line[i/4]+=lines;
            }

          last[i/4]=now;
         }

       switch(i%4)
         {
         case 0:
          output->graph_value=PROCMETER_GRAPH_FLOATING((double)size[i/4]/(1024.0*output->graph_scale));
          sprintf(output->text_value,"%.1f KB",(double)size[i/4]/1024);
          break;
         case 1:
          output->graph_value=PROCMETER_GRAPH_FLOATING((double)grow[i/4]/(1024.0*output->graph_scale));
          sprintf(output->text_value,"%.2f KB/s",(double)grow[i/4]/1024);
          break;
         case 2:
          output->graph_value=PROCMETER_GRAPH_FLOATING((double)line[i/4]/output->graph_scale);
          sprintf(output->text_value,"%.0f lines",(double)line[i/4]);
          break;
         case 3:
          output->graph_value=PROCMETER_GRAPH_FLOATING((double)rate[i/4]/output->graph_scale);
          sprintf(output->text_value,"%.0f lines/s",(double)rate[i/4]);
          break;
         }

       return(0);
      }

 return(-1);
}


/*++++++++++++++++++++++++++++++++++++++
  Tidy up and prepare to have the module unloaded.
  ++++++++++++++++++++++++++++++++++++++*/

void Unload(void)
{
 int i;

 if(outputs)
   {
    for(i=0;outputs[i];i++)
      {
       free(outputs[i]->description);
       free(outputs[i]);
      }
    free(outputs);
   }

 if(file)
   {
    for(i=0;i<nfiles;i++)
       free(file[i]);
    free(file);
    free(last);
    free(mtime);
    free(size);
    free(grow);
    free(line);
    free(rate);
   }
}