cmd PUTNOTIF time option: handle double values.
[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 #include "utils_parse_option.h"
27
28 #define print_to_socket(fh, ...) \
29   if (fprintf (fh, __VA_ARGS__) < 0) { \
30     char errbuf[1024]; \
31     WARNING ("handle_putnotif: failed to write to socket #%i: %s", \
32         fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \
33     return -1; \
34   }
35
36 static int set_option_severity (notification_t *n, const char *value)
37 {
38   if (strcasecmp (value, "Failure") == 0)
39     n->severity = NOTIF_FAILURE;
40   else if (strcasecmp (value, "Warning") == 0)
41     n->severity = NOTIF_WARNING;
42   else if (strcasecmp (value, "Okay") == 0)
43     n->severity = NOTIF_OKAY;
44   else
45     return (-1);
46
47   return (0);
48 } /* int set_option_severity */
49
50 static int set_option_time (notification_t *n, const char *value)
51 {
52   char *endptr = NULL;
53   double tmp;
54
55   errno = 0;
56   tmp = strtod (value, &endptr);
57   if ((errno != 0)         /* Overflow */
58       || (endptr == value) /* Invalid string */
59       || (endptr == NULL)  /* This should not happen */
60       || (*endptr != 0))   /* Trailing chars */
61     return (-1);
62
63   n->time = DOUBLE_TO_CDTIME_T (tmp);
64
65   return (0);
66 } /* int set_option_time */
67
68 static int set_option (notification_t *n, const char *option, const char *value)
69 {
70   if ((n == NULL) || (option == NULL) || (value == NULL))
71     return (-1);
72
73   DEBUG ("utils_cmd_putnotif: set_option (option = %s, value = %s);",
74       option, value);
75
76   if (strcasecmp ("severity", option) == 0)
77     return (set_option_severity (n, value));
78   else if (strcasecmp ("time", option) == 0)
79     return (set_option_time (n, value));
80   else if (strcasecmp ("message", option) == 0)
81     sstrncpy (n->message, value, sizeof (n->message));
82   else if (strcasecmp ("host", option) == 0)
83     sstrncpy (n->host, value, sizeof (n->host));
84   else if (strcasecmp ("plugin", option) == 0)
85     sstrncpy (n->plugin, value, sizeof (n->plugin));
86   else if (strcasecmp ("plugin_instance", option) == 0)
87     sstrncpy (n->plugin_instance, value, sizeof (n->plugin_instance));
88   else if (strcasecmp ("type", option) == 0)
89     sstrncpy (n->type, value, sizeof (n->type));
90   else if (strcasecmp ("type_instance", option) == 0)
91     sstrncpy (n->type_instance, value, sizeof (n->type_instance));
92   else
93     return (1);
94
95   return (0);
96 } /* int set_option */
97
98 int handle_putnotif (FILE *fh, char *buffer)
99 {
100   char *command;
101   notification_t n;
102   int status;
103
104   if ((fh == NULL) || (buffer == NULL))
105     return (-1);
106
107   DEBUG ("utils_cmd_putnotif: handle_putnotif (fh = %p, buffer = %s);",
108       (void *) fh, buffer);
109
110   command = NULL;
111   status = parse_string (&buffer, &command);
112   if (status != 0)
113   {
114     print_to_socket (fh, "-1 Cannot parse command.\n");
115     return (-1);
116   }
117   assert (command != NULL);
118
119   if (strcasecmp ("PUTNOTIF", command) != 0)
120   {
121     print_to_socket (fh, "-1 Unexpected command: `%s'.\n", command);
122     return (-1);
123   }
124
125   memset (&n, '\0', sizeof (n));
126
127   status = 0;
128   while (*buffer != 0)
129   {
130     char *key;
131     char *value;
132
133     status = parse_option (&buffer, &key, &value);
134     if (status != 0)
135     {
136       print_to_socket (fh, "-1 Malformed option.\n");
137       break;
138     }
139
140     status = set_option (&n, key, value);
141     if (status != 0)
142     {
143       print_to_socket (fh, "-1 Error parsing option `%s'\n", key);
144       break;
145     }
146   } /* for (i) */
147
148   /* Check for required fields and complain if anything is missing. */
149   if ((status == 0) && (n.severity == 0))
150   {
151     print_to_socket (fh, "-1 Option `severity' missing.\n");
152     status = -1;
153   }
154   if ((status == 0) && (n.time == 0))
155   {
156     print_to_socket (fh, "-1 Option `time' missing.\n");
157     status = -1;
158   }
159   if ((status == 0) && (strlen (n.message) == 0))
160   {
161     print_to_socket (fh, "-1 No message or message of length 0 given.\n");
162     status = -1;
163   }
164
165   /* If status is still zero the notification is fine and we can finally
166    * dispatch it. */
167   if (status == 0)
168   {
169     plugin_dispatch_notification (&n);
170     print_to_socket (fh, "0 Success\n");
171   }
172
173   return (0);
174 } /* int handle_putnotif */
175
176 /* vim: set shiftwidth=2 softtabstop=2 tabstop=8 : */