Merge branch 'collectd-4.3' into collectd-4.4
[collectd.git] / src / utils_cmd_putnotif.c
1 /**
2  * collectd - src/utils_cms_putnotif.c
3  * Copyright (C) 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  * Author:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #define print_to_socket(fh, ...) \
27   if (fprintf (fh, __VA_ARGS__) < 0) { \
28     char errbuf[1024]; \
29     WARNING ("handle_putnotif: failed to write to socket #%i: %s", \
30         fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
31     return -1; \
32   }
33
34 static int parse_option_severity (notification_t *n, char *value)
35 {
36   if (strcasecmp (value, "Failure") == 0)
37     n->severity = NOTIF_FAILURE;
38   else if (strcasecmp (value, "Warning") == 0)
39     n->severity = NOTIF_WARNING;
40   else if (strcasecmp (value, "Okay") == 0)
41     n->severity = NOTIF_OKAY;
42   else
43     return (-1);
44
45   return (0);
46 } /* int parse_option_severity */
47
48 static int parse_option_time (notification_t *n, char *value)
49 {
50   time_t tmp;
51   
52   tmp = (time_t) atoi (value);
53   if (tmp <= 0)
54     return (-1);
55
56   n->time = tmp;
57
58   return (0);
59 } /* int parse_option_time */
60
61 static int parse_option (notification_t *n, char *buffer)
62 {
63   char *option = buffer;
64   char *value;
65
66   if ((n == NULL) || (option == NULL))
67     return (-1);
68
69   value = strchr (option, '=');
70   if (value == NULL)
71     return (-1);
72   *value = '\0'; value++;
73
74   if (strcasecmp ("severity", option) == 0)
75     return (parse_option_severity (n, value));
76   else if (strcasecmp ("time", option) == 0)
77     return (parse_option_time (n, value));
78   else if (strcasecmp ("host", option) == 0)
79     sstrncpy (n->host, value, sizeof (n->host));
80   else if (strcasecmp ("plugin", option) == 0)
81     sstrncpy (n->plugin, value, sizeof (n->plugin));
82   else if (strcasecmp ("plugin_instance", option) == 0)
83     sstrncpy (n->plugin_instance, value, sizeof (n->plugin_instance));
84   else if (strcasecmp ("type", option) == 0)
85     sstrncpy (n->type, value, sizeof (n->type));
86   else if (strcasecmp ("type_instance", option) == 0)
87     sstrncpy (n->type_instance, value, sizeof (n->type_instance));
88   else
89     return (1);
90
91   return (0);
92 } /* int parse_option */
93
94 static int parse_message (notification_t *n, char **fields, int fields_num)
95 {
96   int status;
97
98   /* Strip off the leading `message=' */
99   fields[0] += strlen ("message=");
100
101   status = strjoin (n->message, sizeof (n->message), fields, fields_num, " ");
102   if (status < 0)
103     return (-1);
104
105   return (0);
106 } /* int parse_message */
107
108 int handle_putnotif (FILE *fh, char **fields, int fields_num)
109 {
110   notification_t n;
111   int status;
112   int i;
113
114   /* Required fields: `PUTNOTIF', severity, time, message */
115   if (fields_num < 4)
116   {
117     DEBUG ("cmd putnotif: Wrong number of fields: %i", fields_num);
118     print_to_socket (fh, "-1 Wrong number of fields: Got %i, "
119         "expected at least 4.\n",
120         fields_num);
121     return (-1);
122   }
123
124   memset (&n, '\0', sizeof (n));
125
126   status = 0;
127   for (i = 1; i < fields_num; i++)
128   {
129     if (strncasecmp (fields[i], "message=", strlen ("message=")) == 0)
130     {
131       status = parse_message (&n, fields + i, fields_num - i);
132       if (status != 0)
133       {
134         print_to_socket (fh, "-1 Error parsing the message. Have you hit the "
135             "limit of %u bytes?\n", (unsigned int) sizeof (n.message));
136       }
137       break;
138     }
139     else
140     {
141       status = parse_option (&n, fields[i]);
142       if (status != 0)
143       {
144         print_to_socket (fh, "-1 Error parsing option `%s'\n", fields[i]);
145         break;
146       }
147     }
148   } /* for (i) */
149
150   /* Check for required fields and complain if anything is missing. */
151   if ((status == 0) && (n.severity == 0))
152   {
153     print_to_socket (fh, "-1 Option `severity' missing.\n");
154     status = -1;
155   }
156   if ((status == 0) && (n.time == 0))
157   {
158     print_to_socket (fh, "-1 Option `time' missing.\n");
159     status = -1;
160   }
161   if ((status == 0) && (strlen (n.message) == 0))
162   {
163     print_to_socket (fh, "-1 No message or message of length 0 given.\n");
164     status = -1;
165   }
166
167   /* If status is still zero the notification is fine and we can finally
168    * dispatch it. */
169   if (status == 0)
170   {
171     plugin_dispatch_notification (&n);
172     print_to_socket (fh, "0 Success\n");
173   }
174
175   return (0);
176 } /* int handle_putnotif */
177
178 /* vim: set shiftwidth=2 softtabstop=2 tabstop=8 : */