-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocan.c
562 lines (513 loc) · 16.7 KB
/
procan.c
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/* Copyright (c) 2007, Matthew W. Jones
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* An intelligent user process analysis tool for FreeBSD, OpenBSD, or Linux
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <paths.h>
#include <string.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <pthread.h>
#include <signal.h>
#include "procan.h"
#include "backend.h"
#include "cli.h"
#if defined (__FreeBSD__)
#include "freebsd_collector.h"
#elif defined (__OpenBSD__)
#include "openbsd_collector.h"
#elif defined (linux)
#include "linux_collector.h"
#else
#error Could not determine your operating system. Make sure it is supported.
#endif
/* Globals with their respective mutexes */
pthread_mutex_t hangup_mutex;
int m_hangup = 0;
pthread_mutex_t procsnap_mutex;
proc_statistics *procsnap;
int numprocsnap = 0;
pthread_mutex_t procchart_mutex;
proc_averages *procavs;
int numprocavs = 0;
pthread_mutex_t pconfig_mutex;
procan_config *pc;
int *bes;
/* Used to signal to the analyzer to use script output or human-readable output */
int scriptoutput = 0;
/* Sorts processes and generates and sorts userlist */
int get_statistics(int *mis, int *uis, int *numints)
{
int holder, i, j;
int numids = 0;
for (i = 0; i < numprocavs; i++)
{
mis[i] = i;
int found = 0;
for (j = 0; j < numids; j++)
{
if (uis[j] == procavs[i].uid)
{
numints[j]+=procavs[i].num_intrests;
found = 1;
break;
}
}
if (!found)
{
uis[numids] = procavs[i].uid;
numints[numids] = procavs[i].num_intrests;
numids++;
}
}
for (i = 0; i < numids; i++)
{
for (j = 0; j < numids; j++)
{
if (numints[j] > numints[j-1])
{
holder = numints[j];
numints[j] = numints[j-1];
numints[j-1] = holder;
holder = uis[j];
uis[j] = uis[j-1];
uis[j-1] = holder;
}
}
}
for (i = 1; i < numprocavs; i++)
{
for (j = 1; j < numprocavs; j++)
{
if (procavs[mis[j]].intrest_score >
procavs[mis[j-1]].intrest_score)
{
holder = mis[j];
mis[j] = mis[j-1];
mis[j-1] = holder;
}
}
}
return numids;
}
/* Fetches a long string with the top 5 processes and why they are the top 5
* Will also display the top 5 most interesting users.
* Calling function must free
*/
char* get_statistics_str()
{
int mis[numprocavs];
int uis[numprocavs];
int numints[numprocavs];
int numids, holder, i, j;
char *nowstats;
char thenstats[50];
if ((nowstats = malloc(sizeof(nowstats) * 300)) == NULL)
{
printf("malloc error, can not allocate memory.\n");
exit(-1);
}
numids = 0;
for (i = 0; i < numprocavs; i++)
{
mis[i] = i;
int found=0;
for (j = 0; j < numids; j++)
{
if (uis[j] == procavs[i].uid)
{
numints[j]+=procavs[i].num_intrests;
found = 1;
break;
}
}
if (!found)
{
uis[numids] = procavs[i].uid;
numints[numids] = procavs[i].num_intrests;
numids++;
}
}
for (i = (numids-1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numints[j-1] > numints[j])
{
holder = numints[j-1];
numints[j-1] = numints[j];
numints[j] = holder;
holder = uis[j-1];
uis[j-1] = uis[j];
uis[j] = holder;
}
}
}
for (i = (numprocavs-1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (procavs[mis[j-1]].num_intrests > procavs[mis[j]].num_intrests)
{
holder = mis[j-1];
mis[j-1] = mis[j];
mis[j] = holder;
}
}
}
int place = 0;
for (i = numprocavs-1; i >= numprocavs-5; i--)
{
if (procavs[mis[i]].num_intrests < 1)
continue;
place++;
snprintf(thenstats,50,"%i: %s (%i) because of %s %s %s\n",
place,
procavs[mis[i]].command,
procavs[mis[i]].lastpid,
(procavs[mis[i]].pintrests > procavs[mis[i]].mintrests) ? "process load." : "memory usage.",
(procavs[mis[i]].swarned || procavs[mis[i]].dwarned || procavs[mis[i]].mwarned) ? "*WARNED*" : "",
(procavs[mis[i]].salarmed || procavs[mis[i]].dalarmed || procavs[mis[i]].malarmed) ? "*ALARMED*" : "");
nowstats = strncat(nowstats, (const char *)thenstats, 50);
}
nowstats = strncat(nowstats, "\nTop 5 users:\n", 14);
place = 0;
for (i = numids-1; i >= numids-5; i--)
{
if (numints[i] < 1)
continue;
place++;
snprintf(thenstats,50,"%i: %i with total interest value of: %i\n",
place,
uis[i],
numints[i]);
nowstats = strncat(nowstats, (const char *)thenstats, 50);
}
return nowstats;
}
int pipe_mode()
{
pthread_t *threads;
int i, e;
scriptoutput = 1;
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, handle_sig);
signal(SIGTERM, handle_sig);
signal(SIGUSR1, handle_sig);
pthread_mutex_init(&procsnap_mutex,NULL);
pthread_mutex_init(&procchart_mutex,NULL);
pthread_mutex_init(&hangup_mutex,NULL);
pthread_mutex_init(&pconfig_mutex,NULL);
if ((threads = (pthread_t *)malloc(2*sizeof(*threads))) == NULL)
{
printf("malloc error, can not allocate memory.\n");
exit(-1);
}
if (( e = pthread_create(&threads[0], NULL, collector_thread, NULL)) != 0)
printf("collector experienced a pthread error: %i\n",e);
if (( e = pthread_create(&threads[1], NULL, analyzer_thread, NULL)) != 0)
printf("analyzer experienced a pthread error: %i\n",e);
pthread_mutex_lock(&hangup_mutex);
while (m_hangup != 1)
{
sleep(2); /* Pipe mode output is handled in the analyzer thread */
}
pthread_mutex_lock(&hangup_mutex);
m_hangup=1;
pthread_mutex_unlock(&hangup_mutex);
pthread_join(threads[0],NULL);
pthread_join(threads[1],NULL);
pthread_mutex_destroy(&hangup_mutex);
pthread_mutex_destroy(&procsnap_mutex);
pthread_mutex_destroy(&procchart_mutex);
pthread_mutex_destroy(&pconfig_mutex);
free(threads);
#if defined (linux)
for (i = 0; i < numprocsnap; i++)
{
if (procsnap[i]._command != NULL)
free(procsnap[i]._command);
}
#endif
free(procsnap);
for (i = 0; i < numprocavs; i++)
{
if (procavs[i].command != NULL)
free(procavs[i].command);
}
free(procavs);
return 0;
}
/*
Housekeeping is performed on all processes that are active.
This includes resetting the hourly_intrests flag and warn and alarm flags
*/
void perform_housekeeping(long current)
{
int i;
pthread_mutex_lock(&procchart_mutex);
for (i = 0; i < numprocavs; i++)
{
if (procavs[i].last_interest_time > 0 &&
(current - procavs[i].last_interest_time) > 3600)
{
if (procavs[i].intrest_score > 0)
procavs[i].intrest_score = procavs[i].intrest_score / 2;
if (procavs[i].num_intrests > 0)
procavs[i].num_intrests = procavs[i].num_intrests / 2;
procavs[i].num_intrests = 0;
procavs[i].interest_threshold = DEFAULT_INTEREST_THRESHOLD;
procavs[i].mintrests = 0;
procavs[i].pintrests = 0;
procavs[i].mwarned = 0;
procavs[i].malarmed = 0;
procavs[i].swarned = 0;
procavs[i].salarmed = 0;
procavs[i].dwarned = 0;
procavs[i].dalarmed = 0;
procavs[i].last_interest_time = current;
}
}
pthread_mutex_unlock(&procchart_mutex);
}
/* Reset statistics and collections */
void reset_statistics()
{
int i;
pthread_mutex_lock(&procchart_mutex);
for (i = 0; i < numprocavs; i++)
{
procavs[i].mwarned = 0;
procavs[i].malarmed = 0;
procavs[i].swarned = 0;
procavs[i].salarmed = 0;
procavs[i].dwarned = 0;
procavs[i].dalarmed = 0;
procavs[i].intrest_score = 0;
procavs[i].num_intrests = 0;
procavs[i].mintrests = 0;
procavs[i].pintrests = 0;
}
pthread_mutex_unlock(&procchart_mutex);
}
int should_ignore_proc(char *name)
{
int i;
for (i = 0; i < pc->nclusions; i++)
{
if (strncmp(pc->exclusions[i], name, strlen(pc->exclusions[i])) == 0)
return 1;
}
return 0;
}
int should_ignore_uid(int uid)
{
int i;
for (i = 0; i < pc->nuids; i++)
{
if (uid == pc->euids[i])
return 1;
}
return 0;
}
/* Daemon mode detaches from the shell, fork() is called from main */
int daemon_mode()
{
pthread_t *threads;
int i,e;
if (setsid() < 0)
{
free_config(pc);
exit(1);
}
for(i = getdtablesize(); i >= 0; --i)
close(i);
i = open("/dev/null",O_RDWR);
dup(i);
dup(i);
umask(027);
chdir("/");
/* The 3 signals we watch for, and ignore the return value of children */
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, handle_sig);
signal(SIGTERM, handle_sig);
signal(SIGUSR1, handle_sig);
pthread_mutex_init(&procsnap_mutex,NULL);
pthread_mutex_init(&procchart_mutex,NULL);
pthread_mutex_init(&hangup_mutex,NULL);
pthread_mutex_init(&pconfig_mutex,NULL);
if ((threads = (pthread_t *)malloc(2*sizeof(*threads))) == NULL)
{
printf("malloc error, can not allocate memory.\n");
exit(-1);
}
if (( e = pthread_create(&threads[0], NULL, collector_thread, NULL)) != 0)
exit(0);
if (( e = pthread_create(&threads[1], NULL, analyzer_thread, NULL)) != 0)
exit(0);
pthread_mutex_lock(&hangup_mutex);
while (m_hangup != 1)
{
pthread_mutex_unlock(&hangup_mutex);
sleep(2);
pthread_mutex_lock(&hangup_mutex);
}
pthread_join(threads[0],NULL);
pthread_join(threads[1],NULL);
pthread_mutex_destroy(&hangup_mutex);
pthread_mutex_destroy(&procsnap_mutex);
pthread_mutex_destroy(&procchart_mutex);
pthread_mutex_destroy(&pconfig_mutex);
free(threads);
free(procsnap);
for (i = 0; i < numprocavs; i++)
{
if (procavs[i].command != NULL)
free(procavs[i].command);
}
free(procavs);
return 0;
}
void usage()
{
printf("Usage: procan [options] [-b [backend1 backend2 ...]]\n");
printf("Options:\n");
printf(" -i: Interactive Mode\n");
printf(" -d: Daemon Mode\n");
printf(" -p: Pipe Mode\n");
printf(" -b: Use given backends.\n\n");
printf("Backends:\n");
printf(" mail: Send digest and warning messages to an administrator\n");
printf(" script: Run a script or scripts based on given conditions\n");
printf(" syslog: Log to syslog periodically\n\n");
printf("Interactive Mode Commands:\n");
printf(" q: Quit\n\n");
printf("Author: Matthew W. Jones <[email protected]>\n");
printf("http://matburt.net/projects/procan\n");
}
/* Signal handler, signals registered in main */
void handle_sig(int sig)
{
switch(sig)
{
case SIGHUP:
pthread_mutex_lock(&pconfig_mutex);
free_config(pc);
pc = get_config();
pthread_mutex_unlock(&pconfig_mutex);
break;
case SIGTERM:
pthread_mutex_lock(&hangup_mutex);
m_hangup=1;
pthread_mutex_unlock(&hangup_mutex);
break;
case SIGUSR1:
reset_statistics();
break;
}
}
int main(int argc, char *argv[])
{
int i,j;
int intract = -1;
bes = calloc(3, sizeof(int));
/* Read the command line arguments */
for (i = 0; i < argc; i++)
{
if (strncmp(argv[i], "-i", 2) == 0)
intract = INTERACTIVE_MODE;
else if (strncmp(argv[i], "-d", 2) == 0)
intract = BACKGROUND_MODE;
else if (strncmp(argv[i], "-p", 2) == 0)
intract = PIPE_MODE;
else if (strncmp(argv[i], "-b", 2) == 0)
{
printf("Using backends: ");
for (j = 0; j < ((argc-i+1 < 3) ? argc-i : 3); j++)
{
i++;
if (strncmp(argv[i], "mail", 4) == 0)
{
bes[j] = MAIL_BACKEND;
printf(" mail");
}
else if (strncmp(argv[i], "script", 6) == 0)
{
bes[j] = SCRIPT_BACKEND;
printf(" script");
}
else if (strncmp(argv[i], "syslog", 6) == 0)
{
bes[j] = SYSLOG_BACKEND;
printf(" syslog");
}
else
{
printf("\nUnsupported backend %s, exiting.\n", argv[i]);
free(bes);
exit(-1);
}
}
printf("\n");
}
}
pc = get_config();
if (intract == INTERACTIVE_MODE)
interactive_mode();
else if (intract == PIPE_MODE)
pipe_mode();
else if (intract == BACKGROUND_MODE)
{
pid_t _p = fork();
if (_p < 0)
{
printf("Fork error.\n");
free_config(pc);
exit(1);
}
if (_p > 0)
{
printf("ProcAn Started.\n");
exit(0);
}
else
daemon_mode();
}
else
{
usage();
exit(-1);
}
exit(0);
}