]> granicus.if.org Git - icinga2/blob - lib/compat/compatlogger.cpp
Adjust deprecation removal for compat features
[icinga2] / lib / compat / compatlogger.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "compat/compatlogger.hpp"
21 #include "compat/compatlogger-ti.cpp"
22 #include "icinga/service.hpp"
23 #include "icinga/checkcommand.hpp"
24 #include "icinga/eventcommand.hpp"
25 #include "icinga/notification.hpp"
26 #include "icinga/macroprocessor.hpp"
27 #include "icinga/externalcommandprocessor.hpp"
28 #include "icinga/compatutility.hpp"
29 #include "base/configtype.hpp"
30 #include "base/objectlock.hpp"
31 #include "base/logger.hpp"
32 #include "base/exception.hpp"
33 #include "base/convert.hpp"
34 #include "base/application.hpp"
35 #include "base/utility.hpp"
36 #include "base/statsfunction.hpp"
37 #include <boost/algorithm/string.hpp>
38
39 using namespace icinga;
40
41 REGISTER_TYPE(CompatLogger);
42
43 REGISTER_STATSFUNCTION(CompatLogger, &CompatLogger::StatsFunc);
44
45 void CompatLogger::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
46 {
47         DictionaryData nodes;
48
49         for (const CompatLogger::Ptr& compat_logger : ConfigType::GetObjectsByType<CompatLogger>()) {
50                 nodes.emplace_back(compat_logger->GetName(), 1); // add more stats
51         }
52
53         status->Set("compatlogger", new Dictionary(std::move(nodes)));
54 }
55
56 /**
57  * @threadsafety Always.
58  */
59 void CompatLogger::Start(bool runtimeCreated)
60 {
61         ObjectImpl<CompatLogger>::Start(runtimeCreated);
62
63         Log(LogInformation, "CompatLogger")
64                 << "'" << GetName() << "' started.";
65
66         Log(LogWarning, "CompatLogger")
67                 << "The CompatLogger feature is DEPRECATED and will be removed in Icinga v2.11.";
68
69         Checkable::OnNewCheckResult.connect(std::bind(&CompatLogger::CheckResultHandler, this, _1, _2));
70         Checkable::OnNotificationSentToUser.connect(std::bind(&CompatLogger::NotificationSentHandler, this, _1, _2, _3, _4, _5, _6, _7, _8));
71         Downtime::OnDowntimeTriggered.connect(std::bind(&CompatLogger::TriggerDowntimeHandler, this, _1));
72         Downtime::OnDowntimeRemoved.connect(std::bind(&CompatLogger::RemoveDowntimeHandler, this, _1));
73         Checkable::OnEventCommandExecuted.connect(std::bind(&CompatLogger::EventCommandHandler, this, _1));
74
75         Checkable::OnFlappingChanged.connect(std::bind(&CompatLogger::FlappingChangedHandler, this, _1));
76         Checkable::OnEnableFlappingChanged.connect(std::bind(&CompatLogger::EnableFlappingChangedHandler, this, _1));
77
78         ExternalCommandProcessor::OnNewExternalCommand.connect(std::bind(&CompatLogger::ExternalCommandHandler, this, _2, _3));
79
80         m_RotationTimer = new Timer();
81         m_RotationTimer->OnTimerExpired.connect(std::bind(&CompatLogger::RotationTimerHandler, this));
82         m_RotationTimer->Start();
83
84         ReopenFile(false);
85         ScheduleNextRotation();
86 }
87
88 /**
89  * @threadsafety Always.
90  */
91 void CompatLogger::Stop(bool runtimeRemoved)
92 {
93         Log(LogInformation, "CompatLogger")
94                 << "'" << GetName() << "' stopped.";
95
96         ObjectImpl<CompatLogger>::Stop(runtimeRemoved);
97 }
98
99 /**
100  * @threadsafety Always.
101  */
102 void CompatLogger::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr &cr)
103 {
104         Host::Ptr host;
105         Service::Ptr service;
106         tie(host, service) = GetHostService(checkable);
107
108         Dictionary::Ptr vars_after = cr->GetVarsAfter();
109
110         long state_after = vars_after->Get("state");
111         long stateType_after = vars_after->Get("state_type");
112         long attempt_after = vars_after->Get("attempt");
113         bool reachable_after = vars_after->Get("reachable");
114
115         Dictionary::Ptr vars_before = cr->GetVarsBefore();
116
117         if (vars_before) {
118                 long state_before = vars_before->Get("state");
119                 long stateType_before = vars_before->Get("state_type");
120                 long attempt_before = vars_before->Get("attempt");
121                 bool reachable_before = vars_before->Get("reachable");
122
123                 if (state_before == state_after && stateType_before == stateType_after &&
124                         attempt_before == attempt_after && reachable_before == reachable_after)
125                         return; /* Nothing changed, ignore this checkresult. */
126         }
127
128         String output;
129         if (cr)
130                 output = CompatUtility::GetCheckResultOutput(cr);
131
132         std::ostringstream msgbuf;
133
134         if (service) {
135                 msgbuf << "SERVICE ALERT: "
136                         << host->GetName() << ";"
137                         << service->GetShortName() << ";"
138                         << Service::StateToString(service->GetState()) << ";"
139                         << Service::StateTypeToString(service->GetStateType()) << ";"
140                         << attempt_after << ";"
141                         << output << ""
142                         << "";
143         } else {
144                 String state = Host::StateToString(Host::CalculateState(static_cast<ServiceState>(state_after)));
145
146                 msgbuf << "HOST ALERT: "
147                         << host->GetName() << ";"
148                         << GetHostStateString(host) << ";"
149                         << Host::StateTypeToString(host->GetStateType()) << ";"
150                         << attempt_after << ";"
151                         << output << ""
152                         << "";
153
154         }
155
156         {
157                 ObjectLock olock(this);
158                 WriteLine(msgbuf.str());
159                 Flush();
160         }
161 }
162
163 /**
164  * @threadsafety Always.
165  */
166 void CompatLogger::TriggerDowntimeHandler(const Downtime::Ptr& downtime)
167 {
168         Host::Ptr host;
169         Service::Ptr service;
170         tie(host, service) = GetHostService(downtime->GetCheckable());
171
172         if (!downtime)
173                 return;
174
175         std::ostringstream msgbuf;
176
177         if (service) {
178                 msgbuf << "SERVICE DOWNTIME ALERT: "
179                         << host->GetName() << ";"
180                         << service->GetShortName() << ";"
181                         << "STARTED" << "; "
182                         << "Checkable has entered a period of scheduled downtime."
183                         << "";
184         } else {
185                 msgbuf << "HOST DOWNTIME ALERT: "
186                         << host->GetName() << ";"
187                         << "STARTED" << "; "
188                         << "Checkable has entered a period of scheduled downtime."
189                         << "";
190         }
191
192         {
193                 ObjectLock oLock(this);
194                 WriteLine(msgbuf.str());
195                 Flush();
196         }
197 }
198
199 /**
200  * @threadsafety Always.
201  */
202 void CompatLogger::RemoveDowntimeHandler(const Downtime::Ptr& downtime)
203 {
204         Host::Ptr host;
205         Service::Ptr service;
206         tie(host, service) = GetHostService(downtime->GetCheckable());
207
208         if (!downtime)
209                 return;
210
211         String downtime_output;
212         String downtime_state_str;
213
214         if (downtime->GetWasCancelled()) {
215                 downtime_output = "Scheduled downtime for service has been cancelled.";
216                 downtime_state_str = "CANCELLED";
217         } else {
218                 downtime_output = "Checkable has exited from a period of scheduled downtime.";
219                 downtime_state_str = "STOPPED";
220         }
221
222         std::ostringstream msgbuf;
223
224         if (service) {
225                 msgbuf << "SERVICE DOWNTIME ALERT: "
226                         << host->GetName() << ";"
227                         << service->GetShortName() << ";"
228                         << downtime_state_str << "; "
229                         << downtime_output
230                         << "";
231         } else {
232                 msgbuf << "HOST DOWNTIME ALERT: "
233                         << host->GetName() << ";"
234                         << downtime_state_str << "; "
235                         << downtime_output
236                         << "";
237         }
238
239         {
240                 ObjectLock oLock(this);
241                 WriteLine(msgbuf.str());
242                 Flush();
243         }
244 }
245
246 /**
247  * @threadsafety Always.
248  */
249 void CompatLogger::NotificationSentHandler(const Notification::Ptr& notification, const Checkable::Ptr& checkable,
250         const User::Ptr& user, NotificationType notification_type, CheckResult::Ptr const& cr,
251         const String& author, const String& comment_text, const String& command_name)
252 {
253         Host::Ptr host;
254         Service::Ptr service;
255         tie(host, service) = GetHostService(checkable);
256
257         String notification_type_str = Notification::NotificationTypeToString(notification_type);
258
259         /* override problem notifications with their current state string */
260         if (notification_type == NotificationProblem) {
261                 if (service)
262                         notification_type_str = Service::StateToString(service->GetState());
263                 else
264                         notification_type_str = GetHostStateString(host);
265         }
266
267         String author_comment = "";
268         if (notification_type == NotificationCustom || notification_type == NotificationAcknowledgement) {
269                 author_comment = author + ";" + comment_text;
270         }
271
272         if (!cr)
273                 return;
274
275         String output;
276         if (cr)
277                 output = CompatUtility::GetCheckResultOutput(cr);
278
279         std::ostringstream msgbuf;
280
281         if (service) {
282                 msgbuf << "SERVICE NOTIFICATION: "
283                         << user->GetName() << ";"
284                         << host->GetName() << ";"
285                         << service->GetShortName() << ";"
286                         << notification_type_str << ";"
287                         << command_name << ";"
288                         << output << ";"
289                         << author_comment
290                         << "";
291         } else {
292                 msgbuf << "HOST NOTIFICATION: "
293                         << user->GetName() << ";"
294                         << host->GetName() << ";"
295                         << notification_type_str << " "
296                         << "(" << GetHostStateString(host) << ");"
297                         << command_name << ";"
298                         << output << ";"
299                         << author_comment
300                         << "";
301         }
302
303         {
304                 ObjectLock oLock(this);
305                 WriteLine(msgbuf.str());
306                 Flush();
307         }
308 }
309
310 /**
311  * @threadsafety Always.
312  */
313 void CompatLogger::FlappingChangedHandler(const Checkable::Ptr& checkable)
314 {
315         Host::Ptr host;
316         Service::Ptr service;
317         tie(host, service) = GetHostService(checkable);
318
319         String flapping_state_str;
320         String flapping_output;
321
322         if (checkable->IsFlapping()) {
323                 flapping_output = "Checkable appears to have started flapping (" + Convert::ToString(checkable->GetFlappingCurrent()) + "% change >= " + Convert::ToString(checkable->GetFlappingThresholdHigh()) + "% threshold)";
324                 flapping_state_str = "STARTED";
325         } else {
326                 flapping_output = "Checkable appears to have stopped flapping (" + Convert::ToString(checkable->GetFlappingCurrent()) + "% change < " + Convert::ToString(checkable->GetFlappingThresholdLow()) + "% threshold)";
327                 flapping_state_str = "STOPPED";
328         }
329
330         std::ostringstream msgbuf;
331
332         if (service) {
333                 msgbuf << "SERVICE FLAPPING ALERT: "
334                         << host->GetName() << ";"
335                         << service->GetShortName() << ";"
336                         << flapping_state_str << "; "
337                         << flapping_output
338                         << "";
339         } else {
340                 msgbuf << "HOST FLAPPING ALERT: "
341                         << host->GetName() << ";"
342                         << flapping_state_str << "; "
343                         << flapping_output
344                         << "";
345         }
346
347         {
348                 ObjectLock oLock(this);
349                 WriteLine(msgbuf.str());
350                 Flush();
351         }
352 }
353
354 void CompatLogger::EnableFlappingChangedHandler(const Checkable::Ptr& checkable)
355 {
356         Host::Ptr host;
357         Service::Ptr service;
358         tie(host, service) = GetHostService(checkable);
359
360         if (checkable->GetEnableFlapping())
361                 return;
362
363         String flapping_output = "Flap detection has been disabled";
364         String flapping_state_str = "DISABLED";
365
366         std::ostringstream msgbuf;
367
368         if (service) {
369                 msgbuf << "SERVICE FLAPPING ALERT: "
370                         << host->GetName() << ";"
371                         << service->GetShortName() << ";"
372                         << flapping_state_str << "; "
373                         << flapping_output
374                         << "";
375         } else {
376                 msgbuf << "HOST FLAPPING ALERT: "
377                         << host->GetName() << ";"
378                         << flapping_state_str << "; "
379                         << flapping_output
380                         << "";
381         }
382
383         {
384                 ObjectLock oLock(this);
385                 WriteLine(msgbuf.str());
386                 Flush();
387         }
388 }
389
390 void CompatLogger::ExternalCommandHandler(const String& command, const std::vector<String>& arguments)
391 {
392         std::ostringstream msgbuf;
393         msgbuf << "EXTERNAL COMMAND: "
394                 << command << ";"
395                 << boost::algorithm::join(arguments, ";")
396                 << "";
397
398         {
399                 ObjectLock oLock(this);
400                 WriteLine(msgbuf.str());
401                 Flush();
402         }
403 }
404
405 void CompatLogger::EventCommandHandler(const Checkable::Ptr& checkable)
406 {
407         Host::Ptr host;
408         Service::Ptr service;
409         tie(host, service) = GetHostService(checkable);
410
411         EventCommand::Ptr event_command = checkable->GetEventCommand();
412         String event_command_name = event_command->GetName();
413         long current_attempt = checkable->GetCheckAttempt();
414
415         std::ostringstream msgbuf;
416
417         if (service) {
418                 msgbuf << "SERVICE EVENT HANDLER: "
419                         << host->GetName() << ";"
420                         << service->GetShortName() << ";"
421                         << Service::StateToString(service->GetState()) << ";"
422                         << Service::StateTypeToString(service->GetStateType()) << ";"
423                         << current_attempt << ";"
424                         << event_command_name;
425         } else {
426                 msgbuf << "HOST EVENT HANDLER: "
427                         << host->GetName() << ";"
428                         << GetHostStateString(host) << ";"
429                         << Host::StateTypeToString(host->GetStateType()) << ";"
430                         << current_attempt << ";"
431                         << event_command_name;
432         }
433
434         {
435                 ObjectLock oLock(this);
436                 WriteLine(msgbuf.str());
437                 Flush();
438         }
439 }
440
441 String CompatLogger::GetHostStateString(const Host::Ptr& host)
442 {
443         if (host->GetState() != HostUp && !host->IsReachable())
444                 return "UNREACHABLE"; /* hardcoded compat state */
445
446         return Host::StateToString(host->GetState());
447 }
448
449 void CompatLogger::WriteLine(const String& line)
450 {
451         ASSERT(OwnsLock());
452
453         if (!m_OutputFile.good())
454                 return;
455
456         m_OutputFile << "[" << (long)Utility::GetTime() << "] " << line << "\n";
457 }
458
459 void CompatLogger::Flush()
460 {
461         ASSERT(OwnsLock());
462
463         if (!m_OutputFile.good())
464                 return;
465
466         m_OutputFile << std::flush;
467 }
468
469 /**
470  * @threadsafety Always.
471  */
472 void CompatLogger::ReopenFile(bool rotate)
473 {
474         ObjectLock olock(this);
475
476         String tempFile = GetLogDir() + "/icinga.log";
477
478         if (m_OutputFile) {
479                 m_OutputFile.close();
480
481                 if (rotate) {
482                         String archiveFile = GetLogDir() + "/archives/icinga-" + Utility::FormatDateTime("%m-%d-%Y-%H", Utility::GetTime()) + ".log";
483
484                         Log(LogNotice, "CompatLogger")
485                                 << "Rotating compat log file '" << tempFile << "' -> '" << archiveFile << "'";
486
487                         (void) rename(tempFile.CStr(), archiveFile.CStr());
488                 }
489         }
490
491         m_OutputFile.open(tempFile.CStr(), std::ofstream::app);
492
493         if (!m_OutputFile) {
494                 Log(LogWarning, "CompatLogger")
495                         << "Could not open compat log file '" << tempFile << "' for writing. Log output will be lost.";
496
497                 return;
498         }
499
500         WriteLine("LOG ROTATION: " + GetRotationMethod());
501         WriteLine("LOG VERSION: 2.0");
502
503         for (const Host::Ptr& host : ConfigType::GetObjectsByType<Host>()) {
504                 String output;
505                 CheckResult::Ptr cr = host->GetLastCheckResult();
506
507                 if (cr)
508                         output = CompatUtility::GetCheckResultOutput(cr);
509
510                 std::ostringstream msgbuf;
511                 msgbuf << "CURRENT HOST STATE: "
512                         << host->GetName() << ";"
513                         << GetHostStateString(host) << ";"
514                         << Host::StateTypeToString(host->GetStateType()) << ";"
515                         << host->GetCheckAttempt() << ";"
516                         << output << "";
517
518                 WriteLine(msgbuf.str());
519         }
520
521         for (const Service::Ptr& service : ConfigType::GetObjectsByType<Service>()) {
522                 Host::Ptr host = service->GetHost();
523
524                 String output;
525                 CheckResult::Ptr cr = service->GetLastCheckResult();
526
527                 if (cr)
528                         output = CompatUtility::GetCheckResultOutput(cr);
529
530                 std::ostringstream msgbuf;
531                 msgbuf << "CURRENT SERVICE STATE: "
532                         << host->GetName() << ";"
533                         << service->GetShortName() << ";"
534                         << Service::StateToString(service->GetState()) << ";"
535                         << Service::StateTypeToString(service->GetStateType()) << ";"
536                         << service->GetCheckAttempt() << ";"
537                         << output << "";
538
539                 WriteLine(msgbuf.str());
540         }
541
542         Flush();
543 }
544
545 void CompatLogger::ScheduleNextRotation()
546 {
547         auto now = (time_t)Utility::GetTime();
548         String method = GetRotationMethod();
549
550         tm tmthen;
551
552 #ifdef _MSC_VER
553         tm *temp = localtime(&now);
554
555         if (!temp) {
556                 BOOST_THROW_EXCEPTION(posix_error()
557                         << boost::errinfo_api_function("localtime")
558                         << boost::errinfo_errno(errno));
559         }
560
561         tmthen = *temp;
562 #else /* _MSC_VER */
563         if (!localtime_r(&now, &tmthen)) {
564                 BOOST_THROW_EXCEPTION(posix_error()
565                         << boost::errinfo_api_function("localtime_r")
566                         << boost::errinfo_errno(errno));
567         }
568 #endif /* _MSC_VER */
569
570         tmthen.tm_min = 0;
571         tmthen.tm_sec = 0;
572
573         if (method == "HOURLY") {
574                 tmthen.tm_hour++;
575         } else if (method == "DAILY") {
576                 tmthen.tm_mday++;
577                 tmthen.tm_hour = 0;
578         } else if (method == "WEEKLY") {
579                 tmthen.tm_mday += 7 - tmthen.tm_wday;
580                 tmthen.tm_hour = 0;
581         } else if (method == "MONTHLY") {
582                 tmthen.tm_mon++;
583                 tmthen.tm_mday = 1;
584                 tmthen.tm_hour = 0;
585         }
586
587         time_t ts = mktime(&tmthen);
588
589         Log(LogNotice, "CompatLogger")
590                 << "Rescheduling rotation timer for compat log '"
591                 << GetName() << "' to '" << Utility::FormatDateTime("%Y/%m/%d %H:%M:%S %z", ts) << "'";
592
593         m_RotationTimer->Reschedule(ts);
594 }
595
596 /**
597  * @threadsafety Always.
598  */
599 void CompatLogger::RotationTimerHandler()
600 {
601         try {
602                 ReopenFile(true);
603         } catch (...) {
604                 ScheduleNextRotation();
605
606                 throw;
607         }
608
609         ScheduleNextRotation();
610 }
611
612 void CompatLogger::ValidateRotationMethod(const Lazy<String>& lvalue, const ValidationUtils& utils)
613 {
614         ObjectImpl<CompatLogger>::ValidateRotationMethod(lvalue, utils);
615
616         if (lvalue() != "HOURLY" && lvalue() != "DAILY" &&
617                 lvalue() != "WEEKLY" && lvalue() != "MONTHLY" && lvalue() != "NONE") {
618                 BOOST_THROW_EXCEPTION(ValidationError(this, { "rotation_method" }, "Rotation method '" + lvalue() + "' is invalid."));
619         }
620 }