1 /*
2  * Copyright (c) 2002-2011 Balabit
3  * Copyright (c) 1998-2011 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 
25 #ifndef LOGSOURCE_H_INCLUDED
26 #define LOGSOURCE_H_INCLUDED
27 
28 #include "logpipe.h"
29 #include "stats/stats-registry.h"
30 #include "window-size-counter.h"
31 #include "dynamic-window.h"
32 
33 typedef struct _LogSourceOptions
34 {
35   gint init_window_size;
36   const gchar *group_name;
37   gboolean keep_timestamp;
38   gboolean keep_hostname;
39   gboolean chain_hostnames;
40   HostResolveOptions host_resolve_options;
41   gchar *program_override;
42   gint program_override_len;
43   gchar *host_override;
44   gint host_override_len;
45   LogTagId source_group_tag;
46   gboolean read_old_records;
47   gboolean use_syslogng_pid;
48   GArray *tags;
49   GList *source_queue_callbacks;
50   gint stats_level;
51   gint stats_source;
52 } LogSourceOptions;
53 
54 typedef struct _LogSource LogSource;
55 
56 /**
57  * LogSource:
58  *
59  * This structure encapsulates an object which generates messages without
60  * defining how those messages are accepted by peers. The most prominent
61  * derived class is LogReader which is an extended RFC3164 capable syslog
62  * message processor used everywhere.
63  **/
64 struct _LogSource
65 {
66   LogPipe super;
67   LogSourceOptions *options;
68   gboolean threaded;
69   gchar *name;
70   gchar *stats_id;
71   gchar *stats_instance;
72   WindowSizeCounter window_size;
73   DynamicWindow dynamic_window;
74   gboolean window_initialized;
75   gsize initial_window_size;
76   /* full_window_size = static + dynamic */
77   gsize full_window_size;
78   atomic_gssize window_size_to_be_reclaimed;
79   atomic_gssize pending_reclaimed;
80   StatsCounterItem *stat_window_size;
81   StatsCounterItem *stat_full_window;
82   StatsCounterItem *last_message_seen;
83   StatsCounterItem *recvd_messages;
84   StatsCluster *stat_window_size_cluster;
85   StatsCluster *stat_full_window_cluster;
86 
87   guint32 last_ack_count;
88   guint32 ack_count;
89   glong window_full_sleep_nsec;
90   struct timespec last_ack_rate_time;
91   AckTrackerFactory *ack_tracker_factory;
92   AckTracker *ack_tracker;
93 
94   void (*wakeup)(LogSource *s);
95   void (*schedule_dynamic_window_realloc)(LogSource *s);
96 };
97 
98 static inline gboolean
log_source_free_to_send(LogSource * self)99 log_source_free_to_send(LogSource *self)
100 {
101   return !window_size_counter_suspended(&self->window_size);
102 }
103 
104 static inline gint
log_source_get_init_window_size(LogSource * self)105 log_source_get_init_window_size(LogSource *self)
106 {
107   return self->initial_window_size;
108 }
109 
110 static inline void
log_source_schedule_dynamic_window_realloc(LogSource * s)111 log_source_schedule_dynamic_window_realloc(LogSource *s)
112 {
113   if (!s || !s->schedule_dynamic_window_realloc)
114     return;
115   s->schedule_dynamic_window_realloc(s);
116 }
117 
118 gboolean log_source_init(LogPipe *s);
119 gboolean log_source_deinit(LogPipe *s);
120 
121 void log_source_post(LogSource *self, LogMessage *msg);
122 
123 void log_source_set_options(LogSource *self, LogSourceOptions *options, const gchar *stats_id,
124                             const gchar *stats_instance, gboolean threaded, LogExprNode *expr_node);
125 void log_source_set_ack_tracker_factory(LogSource *self, AckTrackerFactory *factory);
126 void log_source_set_name(LogSource *self, const gchar *name);
127 void log_source_mangle_hostname(LogSource *self, LogMessage *msg);
128 void log_source_init_instance(LogSource *self, GlobalConfig *cfg);
129 void log_source_options_defaults(LogSourceOptions *options);
130 void log_source_options_init(LogSourceOptions *options, GlobalConfig *cfg, const gchar *group_name);
131 void log_source_options_destroy(LogSourceOptions *options);
132 void log_source_options_set_tags(LogSourceOptions *options, GList *tags);
133 void log_source_free(LogPipe *s);
134 void log_source_wakeup(LogSource *self);
135 void log_source_flow_control_adjust(LogSource *self, guint32 window_size_increment);
136 void log_source_flow_control_adjust_when_suspended(LogSource *self, guint32 window_size_increment);
137 void log_source_flow_control_suspend(LogSource *self);
138 void log_source_disable_bookmark_saving(LogSource *self);
139 void log_source_enable_dynamic_window(LogSource *self, DynamicWindowPool *window_ctr);
140 void log_source_dynamic_window_update_statistics(LogSource *self);
141 gboolean log_source_is_dynamic_window_enabled(LogSource *self);
142 
143 void log_source_global_init(void);
144 
145 /* protected */
146 void log_source_dynamic_window_realloc(LogSource *self);
147 
148 #endif
149