exec plugin: Removed the Nagios logic from the plugin.
[collectd.git] / src / exec.c
1 /**
2  * collectd - src/exec.c
3  * Copyright (C) 2007,2008  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #include "utils_cmd_putval.h"
27 #include "utils_cmd_putnotif.h"
28
29 #include <sys/types.h>
30 #include <pwd.h>
31 #include <grp.h>
32 #include <signal.h>
33
34 #include <pthread.h>
35
36 #define PL_NORMAL        0x01
37 #define PL_NOTIF_ACTION  0x02
38
39 #define PL_RUNNING       0x10
40
41 /*
42  * Private data types
43  */
44 /*
45  * Access to this structure is serialized using the `pl_lock' lock and the
46  * `PL_RUNNING' flag. The execution of notifications is *not* serialized, so
47  * all functions used to handle notifications MUST NOT write to this structure.
48  * The `pid' and `status' fields are thus unused if the `PL_NOTIF_ACTION' flag
49  * is set.
50  * The `PL_RUNNING' flag is set in `exec_read' and unset in `exec_read_one'.
51  */
52 struct program_list_s;
53 typedef struct program_list_s program_list_t;
54 struct program_list_s
55 {
56   char           *user;
57   char           *group;
58   char           *exec;
59   char          **argv;
60   int             pid;
61   int             status;
62   int             flags;
63   program_list_t *next;
64 };
65
66 typedef struct program_list_and_notification_s
67 {
68   program_list_t *pl;
69   notification_t n;
70 } program_list_and_notification_t;
71
72 /*
73  * Private variables
74  */
75 static program_list_t *pl_head = NULL;
76 static pthread_mutex_t pl_lock = PTHREAD_MUTEX_INITIALIZER;
77
78 /*
79  * Functions
80  */
81 static void sigchld_handler (int signal) /* {{{ */
82 {
83   pid_t pid;
84   int status;
85   while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
86   {
87     program_list_t *pl;
88     for (pl = pl_head; pl != NULL; pl = pl->next)
89       if (pl->pid == pid)
90         break;
91     if (pl != NULL)
92       pl->status = status;
93   } /* while (waitpid) */
94 } /* void sigchld_handler }}} */
95
96 static int exec_config_exec (oconfig_item_t *ci) /* {{{ */
97 {
98   program_list_t *pl;
99   char buffer[128];
100   int i;
101
102   if (ci->children_num != 0)
103   {
104     WARNING ("exec plugin: The config option `%s' may not be a block.",
105         ci->key);
106     return (-1);
107   }
108   if (ci->values_num < 2)
109   {
110     WARNING ("exec plugin: The config option `%s' needs at least two "
111         "arguments.", ci->key);
112     return (-1);
113   }
114   if ((ci->values[0].type != OCONFIG_TYPE_STRING)
115       || (ci->values[1].type != OCONFIG_TYPE_STRING))
116   {
117     WARNING ("exec plugin: The first two arguments to the `%s' option must "
118         "be string arguments.", ci->key);
119     return (-1);
120   }
121
122   pl = (program_list_t *) malloc (sizeof (program_list_t));
123   if (pl == NULL)
124   {
125     ERROR ("exec plugin: malloc failed.");
126     return (-1);
127   }
128   memset (pl, '\0', sizeof (program_list_t));
129
130   if (strcasecmp ("NotificationExec", ci->key) == 0)
131     pl->flags |= PL_NOTIF_ACTION;
132   else
133     pl->flags |= PL_NORMAL;
134
135   pl->user = strdup (ci->values[0].value.string);
136   if (pl->user == NULL)
137   {
138     ERROR ("exec plugin: strdup failed.");
139     sfree (pl);
140     return (-1);
141   }
142
143   pl->group = strchr (pl->user, ':');
144   if (pl->group != NULL)
145   {
146     *pl->group = '\0';
147     pl->group++;
148   }
149
150   pl->exec = strdup (ci->values[1].value.string);
151   if (pl->exec == NULL)
152   {
153     ERROR ("exec plugin: strdup failed.");
154     sfree (pl->user);
155     sfree (pl);
156     return (-1);
157   }
158
159   pl->argv = (char **) malloc (ci->values_num * sizeof (char *));
160   if (pl->argv == NULL)
161   {
162     ERROR ("exec plugin: malloc failed.");
163     sfree (pl->exec);
164     sfree (pl->user);
165     sfree (pl);
166     return (-1);
167   }
168   memset (pl->argv, '\0', ci->values_num * sizeof (char *));
169
170   {
171     char *tmp = strrchr (ci->values[1].value.string, '/');
172     if (tmp == NULL)
173       strncpy (buffer, ci->values[1].value.string, sizeof (buffer));
174     else
175       strncpy (buffer, tmp + 1, sizeof (buffer));
176     buffer[sizeof (buffer) - 1] = '\0';
177   }
178   pl->argv[0] = strdup (buffer);
179   if (pl->argv[0] == NULL)
180   {
181     ERROR ("exec plugin: malloc failed.");
182     sfree (pl->argv);
183     sfree (pl->exec);
184     sfree (pl->user);
185     sfree (pl);
186     return (-1);
187   }
188
189   for (i = 1; i < (ci->values_num - 1); i++)
190   {
191     if (ci->values[i + 1].type == OCONFIG_TYPE_STRING)
192     {
193       pl->argv[i] = strdup (ci->values[i + 1].value.string);
194     }
195     else
196     {
197       if (ci->values[i + 1].type == OCONFIG_TYPE_NUMBER)
198       {
199         snprintf (buffer, sizeof (buffer), "%lf",
200             ci->values[i + 1].value.number);
201       }
202       else
203       {
204         if (ci->values[i + 1].value.boolean)
205           strncpy (buffer, "true", sizeof (buffer));
206         else
207           strncpy (buffer, "false", sizeof (buffer));
208       }
209       buffer[sizeof (buffer) - 1] = '\0';
210
211       pl->argv[i] = strdup (buffer);
212     }
213
214     if (pl->argv[i] == NULL)
215     {
216       ERROR ("exec plugin: strdup failed.");
217       break;
218     }
219   } /* for (i) */
220
221   if (i < (ci->values_num - 1))
222   {
223     while ((--i) >= 0)
224     {
225       sfree (pl->argv[i]);
226     }
227     sfree (pl->argv);
228     sfree (pl->exec);
229     sfree (pl->user);
230     sfree (pl);
231     return (-1);
232   }
233
234   for (i = 0; pl->argv[i] != NULL; i++)
235   {
236     DEBUG ("exec plugin: argv[%i] = %s", i, pl->argv[i]);
237   }
238
239   pl->next = pl_head;
240   pl_head = pl;
241
242   return (0);
243 } /* int exec_config_exec }}} */
244
245 static int exec_config (oconfig_item_t *ci) /* {{{ */
246 {
247   int i;
248
249   for (i = 0; i < ci->children_num; i++)
250   {
251     oconfig_item_t *child = ci->children + i;
252     if ((strcasecmp ("Exec", child->key) == 0)
253         || (strcasecmp ("NotificationExec", child->key) == 0))
254       exec_config_exec (child);
255     else
256     {
257       WARNING ("exec plugin: Unknown config option `%s'.", child->key);
258     }
259   } /* for (i) */
260
261   return (0);
262 } /* int exec_config }}} */
263
264 static void exec_child (program_list_t *pl) /* {{{ */
265 {
266   int status;
267   int uid;
268   int gid;
269   int egid;
270
271   struct passwd *sp_ptr;
272   struct passwd sp;
273   char nambuf[2048];
274   char errbuf[1024];
275
276   sp_ptr = NULL;
277   status = getpwnam_r (pl->user, &sp, nambuf, sizeof (nambuf), &sp_ptr);
278   if (status != 0)
279   {
280     ERROR ("exec plugin: getpwnam_r failed: %s",
281         sstrerror (errno, errbuf, sizeof (errbuf)));
282     exit (-1);
283   }
284   if (sp_ptr == NULL)
285   {
286     ERROR ("exec plugin: No such user: `%s'", pl->user);
287     exit (-1);
288   }
289
290   uid = sp.pw_uid;
291   gid = sp.pw_gid;
292   if (uid == 0)
293   {
294     ERROR ("exec plugin: Cowardly refusing to exec program as root.");
295     exit (-1);
296   }
297
298   /* The group configured in the configfile is set as effective group, because
299    * this way the forked process can (re-)gain the user's primary group. */
300   egid = -1;
301   if (NULL != pl->group)
302   {
303     if ('\0' != *pl->group) {
304       struct group *gr_ptr = NULL;
305       struct group gr;
306
307       status = getgrnam_r (pl->group, &gr, nambuf, sizeof (nambuf), &gr_ptr);
308       if (0 != status)
309       {
310         ERROR ("exec plugin: getgrnam_r failed: %s",
311             sstrerror (errno, errbuf, sizeof (errbuf)));
312         exit (-1);
313       }
314       if (NULL == gr_ptr)
315       {
316         ERROR ("exec plugin: No such group: `%s'", pl->group);
317         exit (-1);
318       }
319
320       egid = gr.gr_gid;
321     }
322     else
323     {
324       egid = gid;
325     }
326   } /* if (pl->group == NULL) */
327
328   status = setgid (gid);
329   if (status != 0)
330   {
331     ERROR ("exec plugin: setgid (%i) failed: %s",
332         gid, sstrerror (errno, errbuf, sizeof (errbuf)));
333     exit (-1);
334   }
335
336   if (egid != -1)
337   {
338     status = setegid (egid);
339     if (status != 0)
340     {
341       ERROR ("exec plugin: setegid (%i) failed: %s",
342           egid, sstrerror (errno, errbuf, sizeof (errbuf)));
343       exit (-1);
344     }
345   }
346
347   status = setuid (uid);
348   if (status != 0)
349   {
350     ERROR ("exec plugin: setuid (%i) failed: %s",
351         uid, sstrerror (errno, errbuf, sizeof (errbuf)));
352     exit (-1);
353   }
354
355   status = execvp (pl->exec, pl->argv);
356
357   ERROR ("exec plugin: exec failed: %s",
358       sstrerror (errno, errbuf, sizeof (errbuf)));
359   exit (-1);
360 } /* void exec_child }}} */
361
362 /*
363  * Creates two pipes (one for reading, ong for writing), forks a child, sets up
364  * the pipes so that fd_in is connected to STDIN of the child and fd_out is
365  * connected to STDOUT and STDERR of the child. Then is calls `exec_child'.
366  */
367 static int fork_child (program_list_t *pl, int *fd_in, int *fd_out) /* {{{ */
368 {
369   int fd_pipe_in[2];
370   int fd_pipe_out[2];
371   int status;
372   int pid;
373
374   if (pl->pid != 0)
375     return (-1);
376
377   status = pipe (fd_pipe_in);
378   if (status != 0)
379   {
380     char errbuf[1024];
381     ERROR ("exec plugin: pipe failed: %s",
382         sstrerror (errno, errbuf, sizeof (errbuf)));
383     return (-1);
384   }
385
386   status = pipe (fd_pipe_out);
387   if (status != 0)
388   {
389     char errbuf[1024];
390     ERROR ("exec plugin: pipe failed: %s",
391         sstrerror (errno, errbuf, sizeof (errbuf)));
392     return (-1);
393   }
394
395   pid = fork ();
396   if (pid < 0)
397   {
398     char errbuf[1024];
399     ERROR ("exec plugin: fork failed: %s",
400         sstrerror (errno, errbuf, sizeof (errbuf)));
401     return (-1);
402   }
403   else if (pid == 0)
404   {
405     close (fd_pipe_in[1]);
406     close (fd_pipe_out[0]);
407
408     /* If the `out' pipe has the filedescriptor STDIN we have to be careful
409      * with the `dup's below. So, if this is the case we have to handle the
410      * `out' pipe first. */
411     if (fd_pipe_out[1] == STDIN_FILENO)
412     {
413       int new_fileno = (fd_pipe_in[0] == STDOUT_FILENO)
414         ? STDERR_FILENO : STDOUT_FILENO;
415       dup2 (fd_pipe_out[1], new_fileno);
416       close (fd_pipe_out[1]);
417       fd_pipe_out[1] = new_fileno;
418     }
419     /* Now `fd_pipe_out[1]' is either `STDOUT' or `STDERR', but definitely not
420      * `STDIN_FILENO'. */
421
422     /* Connect the `in' pipe to STDIN */
423     if (fd_pipe_in[0] != STDIN_FILENO)
424     {
425       dup2 (fd_pipe_in[0], STDIN_FILENO);
426       close (fd_pipe_in[0]);
427       fd_pipe_in[0] = STDIN_FILENO;
428     }
429
430     /* Now connect the `out' pipe to STDOUT and STDERR */
431     if (fd_pipe_out[1] != STDOUT_FILENO)
432       dup2 (fd_pipe_out[1], STDOUT_FILENO);
433     if (fd_pipe_out[1] != STDERR_FILENO)
434       dup2 (fd_pipe_out[1], STDERR_FILENO);
435
436     /* If the pipe has some FD that's something completely different, close it
437      * now. */
438     if ((fd_pipe_out[1] != STDOUT_FILENO) && (fd_pipe_out[1] != STDERR_FILENO))
439     {
440       close (fd_pipe_out[1]);
441       fd_pipe_out[1] = STDOUT_FILENO;
442     }
443
444     exec_child (pl);
445     /* does not return */
446   }
447
448   close (fd_pipe_in[0]);
449   close (fd_pipe_out[1]);
450
451   if (fd_in != NULL)
452     *fd_in = fd_pipe_in[1];
453   else
454     close (fd_pipe_in[1]);
455
456   if (fd_out != NULL)
457     *fd_out = fd_pipe_out[0];
458   else
459     close (fd_pipe_out[0]);
460
461   return (pid);
462 } /* int fork_child }}} */
463
464 static int parse_line (char *buffer) /* {{{ */
465 {
466   char *fields[256];
467   int fields_num;
468
469   fields[0] = "PUTVAL";
470   fields_num = strsplit (buffer, fields + 1, STATIC_ARRAY_SIZE(fields) - 1);
471
472   if (strcasecmp (fields[1], "putval") == 0)
473     return (handle_putval (stdout, fields + 1, fields_num));
474   else if (strcasecmp (fields[1], "putnotif") == 0)
475     return (handle_putnotif (stdout, fields + 1, fields_num));
476
477   /* compatibility code */
478   return (handle_putval (stdout, fields, fields_num + 1));
479 } /* int parse_line }}} */
480
481 static void *exec_read_one (void *arg) /* {{{ */
482 {
483   program_list_t *pl = (program_list_t *) arg;
484   int fd;
485   FILE *fh;
486   char buffer[1024];
487   int status;
488
489   status = fork_child (pl, NULL, &fd);
490   if (status < 0)
491     pthread_exit ((void *) 1);
492   pl->pid = status;
493
494   assert (pl->pid != 0);
495
496   fh = fdopen (fd, "r");
497   if (fh == NULL)
498   {
499     char errbuf[1024];
500     ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
501         sstrerror (errno, errbuf, sizeof (errbuf)));
502     kill (pl->pid, SIGTERM);
503     pl->pid = 0;
504     close (fd);
505     pthread_exit ((void *) 1);
506   }
507
508   buffer[0] = '\0';
509   while (fgets (buffer, sizeof (buffer), fh) != NULL)
510   {
511     int len;
512
513     len = strlen (buffer);
514
515     /* Remove newline from end. */
516     while ((len > 0) && ((buffer[len - 1] == '\n')
517           || (buffer[len - 1] == '\r')))
518       buffer[--len] = '\0';
519
520     DEBUG ("exec plugin: exec_read_one: buffer = %s", buffer);
521
522     parse_line (buffer);
523   } /* while (fgets) */
524
525   fclose (fh);
526
527   if (waitpid (pl->pid, &status, 0) > 0)
528     pl->status = status;
529
530   DEBUG ("exec plugin: Child %i exited with status %i.",
531       (int) pl->pid, pl->status);
532
533   pl->pid = 0;
534
535   pthread_mutex_lock (&pl_lock);
536   pl->flags &= ~PL_RUNNING;
537   pthread_mutex_unlock (&pl_lock);
538
539   pthread_exit ((void *) 0);
540   return (NULL);
541 } /* void *exec_read_one }}} */
542
543 static void *exec_notification_one (void *arg) /* {{{ */
544 {
545   program_list_t *pl = ((program_list_and_notification_t *) arg)->pl;
546   const notification_t *n = &((program_list_and_notification_t *) arg)->n;
547   int fd;
548   FILE *fh;
549   int pid;
550   int status;
551   const char *severity;
552
553   pid = fork_child (pl, &fd, NULL);
554   if (pid < 0) {
555     sfree (arg);
556     pthread_exit ((void *) 1);
557   }
558
559   fh = fdopen (fd, "w");
560   if (fh == NULL)
561   {
562     char errbuf[1024];
563     ERROR ("exec plugin: fdopen (%i) failed: %s", fd,
564         sstrerror (errno, errbuf, sizeof (errbuf)));
565     kill (pl->pid, SIGTERM);
566     pl->pid = 0;
567     close (fd);
568     sfree (arg);
569     pthread_exit ((void *) 1);
570   }
571
572   severity = "FAILURE";
573   if (n->severity == NOTIF_WARNING)
574     severity = "WARNING";
575   else if (n->severity == NOTIF_OKAY)
576     severity = "OKAY";
577
578   fprintf (fh,
579       "Severity: %s\n"
580       "Time: %u\n",
581       severity, (unsigned int) n->time);
582
583   /* Print the optional fields */
584   if (strlen (n->host) > 0)
585     fprintf (fh, "Host: %s\n", n->host);
586   if (strlen (n->plugin) > 0)
587     fprintf (fh, "Plugin: %s\n", n->plugin);
588   if (strlen (n->plugin_instance) > 0)
589     fprintf (fh, "PluginInstance: %s\n", n->plugin_instance);
590   if (strlen (n->type) > 0)
591     fprintf (fh, "Type: %s\n", n->type);
592   if (strlen (n->type_instance) > 0)
593     fprintf (fh, "TypeInstance: %s\n", n->type_instance);
594
595   fprintf (fh, "\n%s\n", n->message);
596
597   fflush (fh);
598   fclose (fh);
599
600   waitpid (pid, &status, 0);
601
602   DEBUG ("exec plugin: Child %i exited with status %i.",
603       pid, status);
604
605   sfree (arg);
606   pthread_exit ((void *) 0);
607   return (NULL);
608 } /* void *exec_notification_one }}} */
609
610 static int exec_init (void) /* {{{ */
611 {
612   struct sigaction sa;
613
614   memset (&sa, '\0', sizeof (sa));
615   sa.sa_handler = sigchld_handler;
616   sigaction (SIGCHLD, &sa, NULL);
617
618   return (0);
619 } /* int exec_init }}} */
620
621 static int exec_read (void) /* {{{ */
622 {
623   program_list_t *pl;
624
625   for (pl = pl_head; pl != NULL; pl = pl->next)
626   {
627     pthread_t t;
628     pthread_attr_t attr;
629
630     /* Only execute `normal' style executables here. */
631     if ((pl->flags & PL_NORMAL) == 0)
632       continue;
633
634     pthread_mutex_lock (&pl_lock);
635     /* Skip if a child is already running. */
636     if ((pl->flags & PL_RUNNING) != 0)
637     {
638       pthread_mutex_unlock (&pl_lock);
639       continue;
640     }
641     pl->flags |= PL_RUNNING;
642     pthread_mutex_unlock (&pl_lock);
643
644     pthread_attr_init (&attr);
645     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
646     pthread_create (&t, &attr, exec_read_one, (void *) pl);
647   } /* for (pl) */
648
649   return (0);
650 } /* int exec_read }}} */
651
652 static int exec_notification (const notification_t *n)
653 {
654   program_list_t *pl;
655   program_list_and_notification_t *pln;
656
657   for (pl = pl_head; pl != NULL; pl = pl->next)
658   {
659     pthread_t t;
660     pthread_attr_t attr;
661
662     /* Only execute `notification' style executables here. */
663     if ((pl->flags & PL_NOTIF_ACTION) == 0)
664       continue;
665
666     /* Skip if a child is already running. */
667     if (pl->pid != 0)
668       continue;
669
670     pln = (program_list_and_notification_t *) malloc (sizeof
671         (program_list_and_notification_t));
672     if (pln == NULL)
673     {
674       ERROR ("exec plugin: malloc failed.");
675       continue;
676     }
677
678     pln->pl = pl;
679     memcpy (&pln->n, n, sizeof (notification_t));
680
681     pthread_attr_init (&attr);
682     pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
683     pthread_create (&t, &attr, exec_notification_one, (void *) pln);
684   } /* for (pl) */
685
686   return (0);
687 } /* int exec_notification */
688
689 static int exec_shutdown (void) /* {{{ */
690 {
691   program_list_t *pl;
692   program_list_t *next;
693
694   pl = pl_head;
695   while (pl != NULL)
696   {
697     next = pl->next;
698
699     if (pl->pid > 0)
700     {
701       kill (pl->pid, SIGTERM);
702       INFO ("exec plugin: Sent SIGTERM to %hu", (unsigned short int) pl->pid);
703     }
704
705     sfree (pl->user);
706     sfree (pl);
707
708     pl = next;
709   } /* while (pl) */
710   pl_head = NULL;
711
712   return (0);
713 } /* int exec_shutdown }}} */
714
715 void module_register (void)
716 {
717   plugin_register_complex_config ("exec", exec_config);
718   plugin_register_init ("exec", exec_init);
719   plugin_register_read ("exec", exec_read);
720   plugin_register_notification ("exec", exec_notification);
721   plugin_register_shutdown ("exec", exec_shutdown);
722 } /* void module_register */
723
724 /*
725  * vim:shiftwidth=2:softtabstop=2:tabstop=8:fdm=marker
726  */