aboutsummaryrefslogtreecommitdiff
path: root/src/tagr.h
blob: 96035f270dfeefd5d0d3ec6c0cd459eaca7f1645 (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* This file is part of tagr.
   Copyright (C) 2000, 2005, 2009 Max Bouglacoff, Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <time.h>
#include <errno.h>
#include <xalloc.h>
#include <sysexits.h>
#include <grecs.h>

#define LOGFACILITY LOG_LOCAL5
#define TAGR_CONFIGFILE SYSCONFDIR "/tagr.conf"
#define TAGR_TEMPLATE SYSCONFDIR "/tagr.tmpl"
#define TAGR_PIDFILE "/var/run/tagr.pid"
#define TAGR_DBNAME "tagr.db"
#define TAGR_DBMODE 0600

#define _(s) s
#define N_(s) s

struct monitor
{
  char *id;
  char *name;
  char *dir;
  char *rate_unit;
  unsigned long max_rate;
  double scale;
  unsigned long ystep;
  int swap;
}; 

extern struct grecs_sockaddr listen_sockaddr;
extern int preprocess_only;
extern int log_to_stderr;
extern char *pidfile;
extern unsigned update_interval;

extern char rateup[];
extern char *user;
extern char *basedir;
extern char *configfile;
extern int port;
extern char *progname;
extern int foreground;
extern char *html_template;
extern char *html_input_file;
extern int html_input_line;
extern int verbose_level;
extern char *rate_unit;

extern int percent_option;
extern int transparent_option;
extern int zero_unknown_option;
extern int fill_incoming_option;
extern int color_background[3];
extern int color_light[3];
extern int color_dark[3];
extern int color_major[3];
extern int color_in[3];
extern int color_out[3];
extern int color_grid[3];
extern int color_in_max[3];
extern int color_out_max[3];
extern int color_percent[3];
extern char **number_suffix;
extern size_t number_suffix_count;

void assign_string (char **pstr, const char *s);
void assign_string_n (char **pstr, const char *s, size_t length);

int readconfig (void);
void config_help (void);
void define_symbol (char *s);
void decode_buffer ();
struct monitor *find_monitor (const char *name);
struct monitor *find_monitor_id (const char *id);

/* html.gram.y */
void begin_eval ();
void end_eval ();
int html_open (char *file);
void html_close ();
int yyerror (char *s);

typedef enum { unspecified_value, numeric_value, string_value } value_type;

union value
{
  double number;
  char *string;
};

typedef void (*value_format_fn) (FILE *, union value, const char *, int);

typedef struct
{
  value_type type;
  int prec;
  char *fmt;
  value_format_fn format;
  union value v;
} pp_value_t;

typedef struct pp_value_list
{
  struct pp_value_list *next;
  char *name;
  pp_value_t value;
} pp_tab_t;

int read_symtab (pp_tab_t **tab, const char *name);
int write_symtab (pp_tab_t *tab, const char *name);
void init_value (pp_value_t *p, value_type type, union value *v);
pp_value_t *add_numeric_value (pp_tab_t ** ptab,
			       const char *name, double number);
pp_value_t *add_string_value (pp_tab_t ** ptab,
			      const char *name, const char *string);
int find_value (char *name, pp_value_t * val);
void free_tab (pp_tab_t ** ptab);
int create_html (pp_tab_t * tab, char *file, char *dest);
int check_template ();

/* Logging */
extern int log_facility;
extern char *log_tag;
extern int log_print_severity;

#define L_DEBUG   0
#define L_INFO    1
#define L_NOTICE  2
#define L_WARNING 3
#define L_ERR     4
#define L_CRIT    5

void init_syslog (char *tag);
void logmsg (int level, const char *fmt, ...);
void die (int code, const char *fmt, ...);
void verbose (int level, const char *fmt, ...);



/* Traffic history */

#define DAY_COUNT (600)		/* 400 samples is 33.33 hours */
#define DAY_SAMPLE (5*60)	/* Sample every 5 minutes */
#define WEEK_COUNT (600)	/* 400 samples is 8.33 days */
#define WEEK_SAMPLE (30*60)	/* Sample every 30 minutes */
#define MONTH_COUNT (600)	/* 400 samples is 33.33 days */
#define MONTH_SAMPLE (2*60*60)	/* Sample every 2 hours */
#define YEAR_COUNT  (2 * 366)	/* 1 sample / day, 366 days, 2 years */
#define YEAR_SAMPLE (24*60*60)	/* Sample every 24 hours */

/* One 'rounding error' per sample period, so add 4 to total */
#define MAX_HISTORY (DAY_COUNT+WEEK_COUNT+MONTH_COUNT+YEAR_COUNT+4+4)

struct traffic_history
{
  double inrate;
  double outrate;
};

typedef struct stat_queue queue_t;
struct stat_queue
{
  int head;
  int tail;
  int size;
  struct traffic_history *queue;
};

struct traffic_sample
{
  time_t time;
  unsigned long in, out;
};

struct avg_acc
{
  time_t time;
  unsigned count;
  double inrate;
  double outrate;
};
  
struct traffic_record
{
  struct traffic_sample last; /* Lastly taken sample */
  struct traffic_history last_rates;
  /* Average accumulators */
  struct avg_acc week_avg;  
  struct avg_acc month_avg; 
  struct avg_acc year_avg;
  /* History arrays */
  queue_t day_hist;
  queue_t week_hist;
  queue_t month_hist;
  queue_t year_hist;
  struct traffic_history history[MAX_HISTORY];
};


/* report.c */
#define TAGR_DB_RD 0
#define TAGR_DB_WR 1
void open_db (int);
void close_db ();
void read_db (struct monitor *mon, struct traffic_record **tr);
void write_db (struct monitor *mon, struct traffic_record *tr);
void list_db (void);
void rebuild (int force);
void tr_init (struct traffic_record *tr);

/* stat.c */
void update_stats (struct monitor *mon, struct traffic_sample *sample,
		   struct traffic_record *tr);
void import (const char *dir);

/* queue.c */
void queue_clear (queue_t *q);
int queue_count (queue_t *q);
struct traffic_history *queue_get_ptr (queue_t *q, int index);
struct traffic_history *queue_get_tail (queue_t *q);
void queue_put (queue_t *q, struct traffic_history *elt);
int queue_xchg (queue_t *q, struct traffic_history *elt);

char *mkfilename (const char *dir, const char *name, const char *suffix);


/* output.c */
void scale_sample (struct monitor *mon,
		   const struct traffic_history *in,
		   struct traffic_history *out);
int update_output (struct monitor *mon, struct traffic_record *tr, 
                   time_t timestamp, int force);


/* graph.c */
typedef struct grid *grid_t;

struct grid_class
{
  void *(*create) (grid_t, void *);
  unsigned long (*next) (grid_t, char **, int *);
  void (*dealloc) (void *);
};

grid_t grid_create (struct grid_class *, queue_t *, unsigned long,
		    unsigned long, void *);
unsigned long grid_next (grid_t, char **, int *);
void grid_destroy (grid_t);

extern struct grid_class grid_class_y;
extern struct grid_class grid_class_x_2h;
extern struct grid_class grid_class_x_wday;
extern struct grid_class grid_class_x_week;
extern struct grid_class grid_class_x_month;

int draw_graph (FILE *fp,
		struct monitor *mon,
		queue_t *dataq, const struct avg_acc *avg, time_t now,
		int xstep, unsigned long xmax,
		int growright,
		struct grid_class *xgrid, struct grid_class *ygrid);


Return to:

Send suggestions and report system problems to the System administrator.