1 /*
2  * Copyright (c) 2002-2013 Balabit
3  * Copyright (c) 1998-2013 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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 GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 #include "msg-stats.h"
25 #include "syslog-names.h"
26 #include "logmsg/logmsg.h"
27 #include "apphook.h"
28 
29 /* Static counters for severities and facilities */
30 /* LOG_DEBUG 0x7 */
31 #define SEVERITY_MAX   (0x7 + 1)
32 /* LOG_LOCAL7 23<<3, one additional slot for "everything-else" counter */
33 #define FACILITY_MAX   (23 + 1 + 1)
34 
35 static StatsCounterItem *severity_counters[SEVERITY_MAX];
36 static StatsCounterItem *facility_counters[FACILITY_MAX];
37 
38 static void
_process_message_pri(guint16 pri)39 _process_message_pri(guint16 pri)
40 {
41   int lpri = LOG_FAC(pri);
42 
43   stats_counter_inc(severity_counters[LOG_PRI(pri)]);
44   if (lpri > (FACILITY_MAX - 1))
45     {
46       /* the large facilities (=facility.other) are collected in the last array item */
47       lpri = FACILITY_MAX - 1;
48     }
49   stats_counter_inc(facility_counters[lpri]);
50 }
51 
52 void
msg_stats_update_counters(const gchar * source_id,const LogMessage * msg)53 msg_stats_update_counters(const gchar *source_id, const LogMessage *msg)
54 {
55   if (stats_check_level(2))
56     {
57       stats_lock();
58 
59       StatsClusterKey sc_key;
60       stats_cluster_logpipe_key_set(&sc_key, SCS_HOST | SCS_SOURCE, NULL, log_msg_get_value(msg, LM_V_HOST, NULL) );
61       stats_register_and_increment_dynamic_counter(2, &sc_key, msg->timestamps[LM_TS_RECVD].ut_sec);
62 
63       if (stats_check_level(3))
64         {
65           stats_cluster_logpipe_key_set(&sc_key, SCS_SENDER | SCS_SOURCE, NULL, log_msg_get_value(msg, LM_V_HOST_FROM, NULL) );
66           stats_register_and_increment_dynamic_counter(3, &sc_key, msg->timestamps[LM_TS_RECVD].ut_sec);
67           stats_cluster_logpipe_key_set(&sc_key, SCS_PROGRAM | SCS_SOURCE, NULL, log_msg_get_value(msg, LM_V_PROGRAM, NULL) );
68           stats_register_and_increment_dynamic_counter(3, &sc_key, msg->timestamps[LM_TS_RECVD].ut_sec);
69 
70           stats_cluster_logpipe_key_set(&sc_key, SCS_HOST | SCS_SOURCE, source_id, log_msg_get_value(msg, LM_V_HOST, NULL));
71           stats_register_and_increment_dynamic_counter(3, &sc_key, msg->timestamps[LM_TS_RECVD].ut_sec);
72           stats_cluster_logpipe_key_set(&sc_key, SCS_SENDER | SCS_SOURCE, source_id, log_msg_get_value(msg, LM_V_HOST_FROM,
73                                         NULL));
74           stats_register_and_increment_dynamic_counter(3, &sc_key, msg->timestamps[LM_TS_RECVD].ut_sec);
75         }
76 
77       stats_unlock();
78     }
79   _process_message_pri(msg->pri);
80 }
81 
82 static void
stats_syslog_reinit(void)83 stats_syslog_reinit(void)
84 {
85   gchar name[11] = "";
86   gint i;
87   StatsClusterKey sc_key;
88 
89   stats_lock();
90   if (stats_check_level(3))
91     {
92       /* we need these counters, register them */
93       for (i = 0; i < SEVERITY_MAX; i++)
94         {
95           g_snprintf(name, sizeof(name), "%d", i);
96           stats_cluster_logpipe_key_set(&sc_key, SCS_SEVERITY | SCS_SOURCE, NULL, name );
97           stats_register_counter(3, &sc_key, SC_TYPE_PROCESSED, &severity_counters[i]);
98         }
99 
100       for (i = 0; i < FACILITY_MAX - 1; i++)
101         {
102           g_snprintf(name, sizeof(name), "%d", i);
103           stats_cluster_logpipe_key_set(&sc_key, SCS_FACILITY | SCS_SOURCE, NULL, name );
104           stats_register_counter(3, &sc_key, SC_TYPE_PROCESSED, &facility_counters[i]);
105         }
106       stats_cluster_logpipe_key_set(&sc_key, SCS_FACILITY | SCS_SOURCE, NULL, "other" );
107       stats_register_counter(3, &sc_key, SC_TYPE_PROCESSED, &facility_counters[FACILITY_MAX - 1]);
108     }
109   else
110     {
111       /* no need for facility/severity counters, unregister them */
112       for (i = 0; i < SEVERITY_MAX; i++)
113         {
114           g_snprintf(name, sizeof(name), "%d", i);
115           stats_cluster_logpipe_key_set(&sc_key, SCS_SEVERITY | SCS_SOURCE, NULL, name );
116           stats_unregister_counter(&sc_key, SC_TYPE_PROCESSED, &severity_counters[i]);
117         }
118 
119       for (i = 0; i < FACILITY_MAX - 1; i++)
120         {
121           g_snprintf(name, sizeof(name), "%d", i);
122           stats_cluster_logpipe_key_set(&sc_key, SCS_FACILITY | SCS_SOURCE, NULL, name );
123           stats_unregister_counter(&sc_key, SC_TYPE_PROCESSED, &facility_counters[i]);
124         }
125       stats_cluster_logpipe_key_set(&sc_key, SCS_FACILITY | SCS_SOURCE, NULL, "other" );
126       stats_unregister_counter(&sc_key, SC_TYPE_PROCESSED, &facility_counters[FACILITY_MAX - 1]);
127     }
128   stats_unlock();
129 }
130 
131 void
msg_stats_init(void)132 msg_stats_init(void)
133 {
134   register_application_hook(AH_CONFIG_CHANGED, (ApplicationHookFunc) stats_syslog_reinit, NULL, AHM_RUN_REPEAT);
135 }
136 
137 void
msg_stats_deinit(void)138 msg_stats_deinit(void)
139 {
140 }
141