summaryrefslogtreecommitdiffabout
path: root/src/graph.c
blob: 1197a7de468c7dbe5e04e36e685c857c802e1e00 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* This file is part of tagr.
   Copyright (C) 2006, 2009 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/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <ctype.h>
#include <errno.h>
#include <gd.h>
#include <gdfonts.h>

#include <tagr.h>

int fill_incoming_option = 1; /* Fill incoming graph */
int percent_option = 0;
int transparent_option = 1;
int zero_unknown_option = 1;

int color_background[3]    = { 245,245,245 };
int color_light[3]         = { 194,194,194 };
int color_dark[3]          = { 100,100,100 };
int color_major[3]         = { 255,0,0 };
int color_in[3]            = { 0,235,12 };
int color_out[3]           = { 0,94,255 };
int color_grid[3]          = { 0,0,0 };
int color_in_max[3]        = { 0,166,33 };
int color_out_max[3]       = { 255,0,255 };
int color_percent[3]       = { 239,159,79 };

int graph_xsize = 460;
int graph_ysize = 100;

int graph_h_margin[2] = { 100, 14 };
int graph_v_margin[2] = { 14, 35 };

/* FIXME: I18N?? */
char *rate_unit = "Bytes per Second";

#define make_color_index(g, ar) \
  gdImageColorAllocate (g, (ar)[0], (ar)[1], (ar)[2])

static void
draw_vtext (gdImagePtr graph, int color, const char *text)
{
  gdImageStringUp (graph, gdFontSmall,
		   8,
		   graph_ysize + graph_v_margin[0] -
		   (graph_ysize - gdFontSmall->w * strlen (text))/2,
		   (unsigned char *) text, color);
}

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)
{
  int x, y;
  int i, n;
  gdImagePtr graph, brush_out, brush_outm, brush_outp;
  int i_background, i_light, i_dark;
  int i_major, i_in, i_out, i_grid, i_inm, i_outm;
  int i_outp, i_outpg;
  double xscale, yscale;
  int dotted_style[3];
  int full_xsize = graph_xsize + graph_h_margin[0] + graph_h_margin[1];
  int full_ysize = graph_ysize + graph_v_margin[0] + graph_v_margin[1];
  grid_t grid;
  unsigned long ymax = mon->max_rate;
  time_t start;
  
  yscale = (double) graph_ysize / ymax;
  xscale = (double) graph_xsize / xmax;
  
#define ytr(y)			    \
  (unsigned long) ((ymax >= (y) ? (ymax - (y)) : ymax) * \
		    yscale + graph_h_margin[1])
#define xtr(x)			    \
  (unsigned long) (growright ?      \
		   ((full_xsize - (x)*xscale)) : \
		    (graph_h_margin[0] + (x)*xscale))

  graph = gdImageCreate (full_xsize, full_ysize);
  brush_out = gdImageCreate (1, 2);
  brush_outm = gdImageCreate (1, 2);
  brush_outp = gdImageCreate (1, 2);

  i_background = make_color_index (graph, color_background);
  i_light = make_color_index (graph, color_light);
  i_dark = make_color_index (graph, color_dark);

  if (transparent_option)
    gdImageColorTransparent (graph, i_background);

  gdImageInterlace (graph, 1);
  
  i_major = make_color_index (graph, color_major);
  i_in = make_color_index (graph, color_in);
  i_out = make_color_index (brush_out, color_out);
  i_grid = make_color_index (graph, color_grid);
  i_inm = make_color_index (graph, color_in_max);
  i_outm = make_color_index (brush_outm, color_out_max);
  i_outp = make_color_index (brush_outp, color_percent);
  i_outpg = make_color_index (graph, color_percent);

  /* Draw the image border */
  gdImageLine (graph, 0, 0, full_xsize - 1, 0, i_light);
  gdImageLine (graph, 1, 1, full_xsize - 2, 1, i_light);
  gdImageLine (graph, 0, 0, 0, full_ysize - 1, i_light);
  gdImageLine (graph, 1, 1, 1, full_ysize - 2, i_light);
  gdImageLine (graph, full_xsize - 1, 0, full_xsize - 1,
	       full_ysize - 1, i_dark);
  gdImageLine (graph, 0, full_ysize - 1, full_xsize - 1,
	       full_ysize - 1, i_dark);
  gdImageLine (graph, full_xsize - 2, 1, full_xsize - 2,
	       full_ysize - 2, i_dark);
  gdImageLine (graph, 1, full_ysize - 2, full_xsize - 2,
	       full_ysize - 2, i_dark);

  n = queue_count (dataq);

  /* Incoming traffic */
  for (i = n - 1, start = 0; i > 0; i--)
    {
      struct traffic_history th, tnext;
      
      scale_sample (mon, queue_get_ptr (dataq, i), &th);
      if (start == 0)
	start = th.time;
      scale_sample (mon, queue_get_ptr (dataq, i - 1), &tnext);
      if (start - tnext.time > xmax)
	break;
      if (zero_unknown_option
	  && th.time - tnext.time > xstep * cut_out_fraction)
	{
	  gdImageLine (graph, xtr (start - tnext.time), ytr (0),
		       xtr (start - tnext.time), ytr (tnext.inrate), i_in);
	  th.inrate = tnext.inrate = 0;
	  th.outrate = tnext.outrate = 0;
	}
	  
      if (fill_incoming_option)
	gdImageLine (graph,
		     xtr (start - th.time), ytr (0),
		     xtr (start - th.time), ytr (tnext.inrate), i_in);
      gdImageLine (graph, xtr (start - th.time), ytr (th.inrate),
		   xtr (start - tnext.time), ytr (tnext.inrate), i_in);
    }

  /* Outgoing traffic */
  gdImageSetBrush (graph, brush_out);
  for (i = n - 1, start = 0; i > 0; i--)
    {
      struct traffic_history th, tnext;
      
      scale_sample (mon, queue_get_ptr (dataq, i), &th);
      if (start == 0)
	start = th.time;
      scale_sample (mon, queue_get_ptr (dataq, i - 1), &tnext);
      if (start - tnext.time > xmax)
	break;

      if (zero_unknown_option
	  && th.time - tnext.time > xstep * cut_out_fraction)
	{
	  gdImageLine (graph, xtr (start - tnext.time), ytr (0),
		       xtr (start - tnext.time), ytr (tnext.inrate),
		       gdBrushed);
	  th.inrate = tnext.inrate = 0;
	  th.outrate = tnext.outrate = 0;
	}
      
      gdImageLine (graph, xtr (start - th.time), ytr (th.outrate),
		   xtr (start - tnext.time), ytr (tnext.outrate), gdBrushed);
    }

  /* Border */
  gdImageRectangle (graph,
                    xtr (0), ytr (0),
                    xtr (xmax), ytr (ymax), i_grid);


  dotted_style[0] = i_grid;
  dotted_style[1] = gdTransparent;
  dotted_style[2] = gdTransparent;
  gdImageSetStyle (graph, dotted_style, 3);

  grid = grid_create (ygrid, dataq, 0, ymax, mon);
  if (grid)
    {
      unsigned long i;
      char *str = NULL;
      
      draw_vtext (graph, i_grid,
		  mon->rate_unit ? mon->rate_unit : rate_unit);

      /* Draw vertical grid */
      while ((i = grid_next (grid, &str, NULL)) < ymax)
	{
	  int y = ytr (i);
	  
	  /* Left tick */
	  gdImageLine (graph,
		       xtr (0) - 2, y,
		       xtr (0) + 1, y,
		       i_grid);
	  /* Right tick */
	  gdImageLine (graph,
		       xtr (xmax) - 1, y,
		       xtr (xmax) + 1, y,
		       i_grid);

	  /* Grid line */
	  gdImageLine (graph, xtr (0), y, xtr (xmax), y,
		       gdStyled);

	  if (str)
	    gdImageString (graph, gdFontSmall,
			     23, y - gdFontSmall->h / 2,
			     (unsigned char *) str, i_grid);
	}
      grid_destroy (grid);
    }
  
  grid = grid_create (xgrid, dataq, 0, xmax, &now);
  if (grid)
    {
      unsigned long i;
      char *str = NULL;
      int mark;
      
      while ((i = grid_next (grid, &str, &mark)) < xmax)
	{
	  int x = xtr (i);
	  
	  /* Tick */
	  gdImageLine (graph,
		       x, ytr (0) - 2, x, ytr (0) + 1,
		       i_grid);
	  /* Grid line */
	  gdImageLine (graph,
		       x, ytr (0), x, ytr (ymax),
		       mark ? i_major : gdStyled);

	  if (str)
	    gdImageString (graph, gdFontSmall,
			   x - strlen (str) * gdFontSmall->w / 2,
			   graph_ysize + graph_v_margin[0] + 2,
			   (unsigned char *) str, i_grid);
	}
      grid_destroy (grid);
    }
  
  /* Mark 0,0 */
  x = graph_h_margin[0];
  y = graph_ysize + graph_h_margin[1];
  gdImageLine (graph, x + 2, y + 3, x + 2, y - 3, i_major);
  gdImageLine (graph, x + 1, y + 3, x + 1, y - 3, i_major);
  gdImageLine (graph, x, y + 2, x, y - 2, i_major);
  gdImageLine (graph, x - 1, y + 1, x - 1, y - 1, i_major);
  gdImageLine (graph, x - 2, y + 1, x - 2, y - 1, i_major);
  gdImageLine (graph, x - 3, y, x - 3, y, i_major);
  
  gdImagePng (graph, fp);
  
  gdImageDestroy (graph);
  gdImageDestroy (brush_out);
  gdImageDestroy (brush_outm);
  gdImageDestroy (brush_outp);
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.