-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotifymail.cpp
More file actions
100 lines (88 loc) · 3.76 KB
/
notifymail.cpp
File metadata and controls
100 lines (88 loc) · 3.76 KB
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
/**
*************************************************************
* @file notifymail.cpp
* @brief Breve descripción
* Pequeña documentación del archivo
*
*
*
*
*
* @author Gaspar Fernández <blakeyed@totaki.com>
* @version
* @date 15 dic 2015
* Historial de cambios:
*
*
*
*
*
*
*
*************************************************************/
#include "notifymail.hpp"
#include <iostream>
#include "lib/timeutils.hpp"
#include "lib/mailer.hpp"
NotifyMail::NotifyMail(std :: string id):Notify(id, "mail")
{
}
NotifyMail::~NotifyMail()
{
}
void NotifyMail::_newFail(const std :: string & serviceName, std :: chrono :: system_clock :: time_point outageStart, int code, const std :: string &message)
{
auto mailBody="System Administrator,\n\n"
"Sermon has detected an error in the following service:\n\n"
+serviceName+"\n\n"
"At: "+timeformat(outageStart)+"\n" +
"Error message: "+message+" ("+std::to_string(code)+")\n\n"
"Please, fix this as soon as possible\n";
/* std::cout << "ITEMIZED TEST: "<<itemized_to_str(get_itemized(std::chrono::seconds(86401))); */
/* std::cout << "ACABA DE FALLAR EL SERVICIO "<<serviceName<<"\n\nA las: "<<timeformat(outageStart)<<"\nCODIGO: "<<code<< " ERR: "<<message<<"\n\n"; */
std::string from = (option.find("from")!=option.end())?option["from"]:"sermonmailer@root";
std::string subject = (option.find("errorSubject")!=option.end())?option["errorSubject"]:"Error in server";
auto debug = option.find("debug");
if (debug == option.end())
{
int statusCode;
if ((statusCode = Mailer::sendmail(from, option["to"], subject, mailBody))<=0)
std::cerr << "Could not send e-mail to administrator!!! (Status code "<<statusCode<<")"<<std::endl;
}
else if (debug->second!="hidden")
{
std::cerr << " ------------------------- SERVICE FAIL MAIL ------------------------"<<std::endl;
std::cerr << mailBody;
std::cerr << " ------------------------- SERVICE FAIL MAIL ------------------------"<<std::endl;
}
}
void NotifyMail::_newBounce(const std::string& serviceName, uint64_t bounces, std::chrono::system_clock::time_point outageStart, std::chrono::system_clock::time_point outageElapsed, int code, const std::string& message)
{
}
void NotifyMail::_newRecovery(const std :: string & serviceName, std :: chrono :: system_clock :: time_point outageStart, std :: chrono :: system_clock :: time_point outageEnd, int code, const std :: string &message)
{
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(outageEnd - outageStart).count();
std::string from = (option.find("from")!=option.end())?option["from"]:"sermonmailer@root";
std::string subject = (option.find("recoverySubject")!=option.end())?option["recoverySubject"]:"Good news! Server recovery";
auto mailBody="System Administrator,\n\n"
"Sermon has detected recovery on the service "+serviceName+" after "+itemized_to_str(get_itemized(std::chrono::seconds(elapsed)))+".\n\n"
"Original error: "+message+" ("+std::to_string(code)+")\n" +
"Started at: "+timeformat(outageStart)+"\n" +
"Ended at: "+timeformat(outageEnd)+"\n\n"
"EVERYTHING IS FINE NOW";
auto debug = option.find("debug");
if (debug == option.end())
{
if (!Mailer::sendmail(from, option["to"], subject, mailBody))
std::cerr << "Could not send e-mail to administrator!!!"<<std::endl;
}
else if (debug->second!="hidden")
{
std::cerr << " ------------------------- SERVICE RECOVERY MAIL ------------------------"<<std::endl;
std::cerr << mailBody;
std::cerr << " ------------------------- SERVICE RECOVERY MAIL ------------------------"<<std::endl;
}
}
void NotifyMail::_newMessage(const std::string& type, const std::string& serviceName, std::chrono::system_clock::time_point outageStart, const std::string& message)
{
}