Merge branch 'collectd-4.4' into collectd-4.5
[collectd.git] / src / notify_email.c
1 /**
2  * collectd - src/notify_email.c
3  * Copyright (C) 2008  Oleg King
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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Oleg King <king2 at kaluga.ru>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #include <auth-client.h>
28 #include <libesmtp.h>
29
30 #define MAXSTRING               256
31
32 static const char *config_keys[] =
33 {
34   "SMTPServer",
35   "SMTPPort",
36   "SMTPUser",
37   "SMTPPassword",
38   "From",
39   "Recipient",
40   "Subject"
41 };
42 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
43
44 static char **recipients;
45 static int recipients_len = 0;
46
47 static smtp_session_t session;
48 static smtp_message_t message;
49 static auth_context_t authctx = NULL;
50
51 static int smtp_port = 25;
52 static char *smtp_host = NULL;
53 static char *smtp_user = NULL;
54 static char *smtp_password = NULL;
55 static char *email_from = NULL;
56 static char *email_subject = NULL;
57
58 #define DEFAULT_SMTP_HOST       "localhost"
59 #define DEFAULT_SMTP_FROM       "root@localhost"
60 #define DEFAULT_SMTP_SUBJECT    "Collectd notify: %s@%s"
61
62 /* Callback to get username and password */
63 static int authinteract (auth_client_request_t request, char **result,
64     int fields, void *arg)
65 {               
66   int i;
67   for (i = 0; i < fields; i++)
68   {
69     if (request[i].flags & AUTH_USER)
70       result[i] = smtp_user;
71     else if (request[i].flags & AUTH_PASS)
72       result[i] = smtp_password;
73     else
74       return 0;
75   }
76   return 1;
77 } /* int authinteract */
78
79 /* Callback to print the recipient status */
80 static void print_recipient_status (smtp_recipient_t recipient,
81     const char *mailbox, void *arg)
82 {
83   const smtp_status_t *status;
84
85   status = smtp_recipient_status (recipient);
86   if (status->text[strlen(status->text) - 2] == '\r')
87     status->text[strlen(status->text) - 2] = 0;
88   INFO ("notify_email: notify sent to %s: %d %s", mailbox, status->code,
89       status->text);
90 } /* void print_recipient_status */
91
92 /* Callback to monitor SMTP activity */
93 static void monitor_cb (const char *buf, int buflen, int writing, void *arg)
94 {
95   char log_str[MAXSTRING];
96
97   sstrncpy (log_str, buf, sizeof (log_str));
98   if (buflen > 2)
99     log_str[buflen - 2] = 0; /* replace \n with \0 */
100
101   if (writing == SMTP_CB_HEADERS) {
102     DEBUG ("notify_email plugin: SMTP --- H: %s", log_str);
103     return;
104   }
105   DEBUG (writing
106       ? "notify_email plugin: SMTP >>> C: %s"
107       : "notify_email plugin: SMTP <<< S: %s",
108       log_str);
109 } /* void monitor_cb */
110
111 static int notify_email_init (void)
112 {
113   char server[MAXSTRING];
114
115   auth_client_init();
116   if (!(session = smtp_create_session ())) {
117     ERROR ("notify_email plugin: cannot create SMTP session");
118     return (-1);
119   }
120
121   smtp_set_monitorcb (session, monitor_cb, NULL, 1);
122   smtp_set_hostname (session, hostname_g);
123   ssnprintf(server, sizeof (server), "%s:%i",
124       (smtp_host == NULL) ? DEFAULT_SMTP_HOST : smtp_host,
125       smtp_port);
126   smtp_set_server (session, server);
127
128   if (smtp_user && smtp_password) {
129     authctx = auth_create_context ();
130     auth_set_mechanism_flags (authctx, AUTH_PLUGIN_PLAIN, 0);
131     auth_set_interact_cb (authctx, authinteract, NULL);
132   }
133
134   if ( !smtp_auth_set_context (session, authctx)) {
135     ERROR ("notify_email plugin: cannot set SMTP auth context");
136     return (-1);   
137   }
138
139   return (0);
140 } /* int notify_email_init */
141
142 static int notify_email_shutdown (void)
143 {
144   smtp_destroy_session (session);
145   auth_destroy_context (authctx);
146   auth_client_exit();
147   return (0);
148 } /* int notify_email_shutdown */
149
150 static int notify_email_config (const char *key, const char *value)
151 {
152   if (strcasecmp (key, "Recipient") == 0)
153   {
154     char **tmp;
155
156     tmp = (char **) realloc ((void *) recipients, (recipients_len + 1) * sizeof (char *));
157     if (tmp == NULL) {
158       ERROR ("notify_email: realloc failed.");
159       return (-1);
160     }
161
162     recipients = tmp;
163     recipients[recipients_len] = strdup (value);
164     if (recipients[recipients_len] == NULL) {
165       ERROR ("notify_email: strdup failed.");
166       return (-1);
167     }
168     recipients_len++;
169   }
170   else if (0 == strcasecmp (key, "SMTPServer")) {
171     sfree (smtp_host);
172     smtp_host = strdup (value);
173   }
174   else if (0 == strcasecmp (key, "SMTPPort")) {
175     int port_tmp = atoi (value);
176     if (port_tmp < 1 || port_tmp > 65535)
177     {
178       WARNING ("notify_email plugin: Invalid SMTP port: %i", port_tmp);
179       return (1);
180     }
181     smtp_port = port_tmp;
182   }
183   else if (0 == strcasecmp (key, "SMTPUser")) {
184     sfree (smtp_user);
185     smtp_user = strdup (value);
186   }
187   else if (0 == strcasecmp (key, "SMTPPassword")) {
188     sfree (smtp_password);
189     smtp_password = strdup (value);
190   }
191   else if (0 == strcasecmp (key, "From")) {
192     sfree (email_from);
193     email_from = strdup (value);
194   }
195   else if (0 == strcasecmp (key, "Subject")) {
196     sfree (email_subject);
197     email_subject = strdup (value);
198   }
199   else {
200     return -1;
201   }
202   return 0;
203 } /* int notify_email_config (const char *, const char *) */
204
205 static int notify_email_notification (const notification_t *n)
206 {
207   smtp_recipient_t recipient;
208
209   struct tm timestamp_tm;
210   char timestamp_str[64];
211
212   char severity[32];
213   char subject[MAXSTRING];
214
215   char buf[4096] = "";
216   int  buf_len = sizeof (buf);
217   int i;
218
219   ssnprintf (severity, sizeof (severity), "%s",
220       (n->severity == NOTIF_FAILURE) ? "FAILURE"
221       : ((n->severity == NOTIF_WARNING) ? "WARNING"
222         : ((n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN")));
223
224   ssnprintf (subject, sizeof (subject),
225       (email_subject == NULL) ? DEFAULT_SMTP_SUBJECT : email_subject,
226       severity, n->host);
227
228   localtime_r (&n->time, &timestamp_tm);
229   strftime (timestamp_str, sizeof (timestamp_str), "%Y-%m-%d %H:%M:%S",
230       &timestamp_tm);
231   timestamp_str[sizeof (timestamp_str) - 1] = '\0';
232
233   /* Let's make RFC822 message text with \r\n EOLs */
234   ssnprintf (buf, buf_len,
235       "MIME-Version: 1.0\r\n"
236       "Content-Type: text/plain;\r\n"
237       "Content-Transfer-Encoding: 8bit\r\n"
238       "Subject: %s\r\n"
239       "\r\n"
240       "%s - %s@%s\r\n"
241       "\r\n"
242       "Message: %s",
243       subject,
244       timestamp_str,
245       severity,
246       n->host,
247       n->message);
248
249   if (!(message = smtp_add_message (session))) {
250     ERROR ("notify_email plugin: cannot set SMTP message");
251     return (-1);   
252   }
253   smtp_set_reverse_path (message, email_from);
254   smtp_set_header (message, "To", NULL, NULL);
255   smtp_set_message_str (message, buf);
256
257   for (i = 0; i < recipients_len; i++)
258     recipient = smtp_add_recipient (message, recipients[i]);
259
260   /* Initiate a connection to the SMTP server and transfer the message. */
261   if (!smtp_start_session (session)) {
262     char buf[MAXSTRING];
263     ERROR ("notify_email plugin: SMTP server problem: %s",
264         smtp_strerror (smtp_errno (), buf, sizeof buf));
265     return (-1);
266   } else {
267     const smtp_status_t *status;
268     /* Report on the success or otherwise of the mail transfer. */
269     status = smtp_message_transfer_status (message);
270     DEBUG ("notify_email plugin: SMTP server report: %d %s",
271         status->code, (status->text != NULL) ? status->text : "\n");
272     smtp_enumerate_recipients (message, print_recipient_status, NULL);
273   }
274
275   return (0);
276 } /* int notify_email_notification */
277
278 void module_register (void)
279 {
280   plugin_register_init ("notify_email", notify_email_init);
281   plugin_register_shutdown ("notify_email", notify_email_shutdown);
282   plugin_register_config ("notify_email", notify_email_config,
283       config_keys, config_keys_num);
284   plugin_register_notification ("notify_email", notify_email_notification);
285 } /* void module_register (void) */
286
287 /* vim: set sw=2 sts=2 ts=8 et : */