NFVBENCH-42 Add multiple fluentd aggregators support
[nfvbench.git] / nfvbench / fluentd.py
1 # Copyright 2017 Cisco Systems, Inc.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 import logging
16
17 from datetime import datetime
18 from fluent import sender
19 import pytz
20
21
22 class FluentLogHandler(logging.Handler):
23     '''This is a minimalist log handler for use with Fluentd
24
25     Needs to be attached to a logger using the addHandler method.
26     It only picks up from every record:
27     - the formatted message (no timestamp and no level)
28     - the level name
29     - the runlogdate (to tie multiple run-related logs together)
30     The timestamp is retrieved by the fluentd library.
31     There will be only one instance of FluentLogHandler running.
32     '''
33
34     def __init__(self, fluentd_configs):
35         logging.Handler.__init__(self)
36         self.log_senders = []
37         self.result_senders = []
38         self.runlogdate = 0
39         self.formatter = logging.Formatter('%(message)s')
40         for fluentd_config in fluentd_configs:
41             if fluentd_config.logging_tag:
42                 self.log_senders.append(
43                     sender.FluentSender(fluentd_config.logging_tag, host=fluentd_config.ip,
44                                         port=fluentd_config.port))
45             if fluentd_config.result_tag:
46                 self.result_senders.append(
47                     sender.FluentSender(fluentd_config.result_tag, host=fluentd_config.ip,
48                                         port=fluentd_config.port))
49         self.__warning_counter = 0
50         self.__error_counter = 0
51
52     def start_new_run(self):
53         '''Delimitate a new run in the stream of records with a new timestamp
54         '''
55         # reset counters
56         self.__warning_counter = 0
57         self.__error_counter = 0
58         self.runlogdate = self.__get_timestamp()
59         # send start record
60         self.__send_start_record()
61
62     def emit(self, record):
63         data = {
64             "loglevel": record.levelname,
65             "message": self.formatter.format(record),
66             "@timestamp": self.__get_timestamp()
67         }
68         # if runlogdate is 0, it's a log from server (not an nfvbench run) so do not send runlogdate
69         if self.runlogdate != 0:
70             data["runlogdate"] = self.runlogdate
71
72         self.__update_stats(record.levelno)
73         for log_sender in self.log_senders:
74             log_sender.emit(None, data)
75
76     # this function is called by summarizer, and used for sending results
77     def record_send(self, record):
78         for result_sender in self.result_senders:
79             result_sender.emit(None, record)
80
81     # send START log record for each run
82     def __send_start_record(self):
83         data = {
84             "runlogdate": self.runlogdate,
85             "loglevel": "START",
86             "message": "NFVBENCH run is started",
87             "numloglevel": 0,
88             "numerrors": 0,
89             "numwarnings": 0,
90             "@timestamp": self.__get_timestamp()
91         }
92         for log_sender in self.log_senders:
93             log_sender.emit(None, data)
94
95     # send stats related to the current run and reset state for a new run
96     def send_run_summary(self, run_summary_required):
97         if run_summary_required or self.__get_highest_level() == logging.ERROR:
98             data = {
99                 "loglevel": "RUN_SUMMARY",
100                 "message": self.__get_highest_level_desc(),
101                 "numloglevel": self.__get_highest_level(),
102                 "numerrors": self.__error_counter,
103                 "numwarnings": self.__warning_counter,
104                 "@timestamp": self.__get_timestamp()
105             }
106             # if runlogdate is 0, it's a log from server (not an nfvbench run)
107             # so don't send runlogdate
108             if self.runlogdate != 0:
109                 data["runlogdate"] = self.runlogdate
110             for log_sender in self.log_senders:
111                 log_sender.emit(None, data)
112
113     def __get_highest_level(self):
114         if self.__error_counter > 0:
115             return logging.ERROR
116         elif self.__warning_counter > 0:
117             return logging.WARNING
118         return logging.INFO
119
120     def __get_highest_level_desc(self):
121         highest_level = self.__get_highest_level()
122         if highest_level == logging.INFO:
123             return "GOOD RUN"
124         elif highest_level == logging.WARNING:
125             return "RUN WITH WARNINGS"
126         return "RUN WITH ERRORS"
127
128     def __update_stats(self, levelno):
129         if levelno == logging.WARNING:
130             self.__warning_counter += 1
131         elif levelno == logging.ERROR:
132             self.__error_counter += 1
133
134     def __get_timestamp(self):
135         return datetime.utcnow().replace(tzinfo=pytz.utc).strftime(
136             "%Y-%m-%dT%H:%M:%S.%f%z")