1 /*
2  * File:      rrdif.h
3  * Copyright: (c) 2006 by Peter Gritsch
4  * Email:     s4nag@no-where.at
5  *
6  * This file is part of SNMP4Nagios.
7  *
8  * SNMP4Nagios is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or (at
11  * your option) any later version.
12  *
13  * SNMP4Nagios is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with SNMP4Nagios; if not, write to the
20  *
21  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22  * MA 02110-1301, USA
23  */
24 
25 #ifndef RRDIF_H
26 #define RRDIF_H
27 
28 #if(WITH_RRD)
29 
30 
31 /**********************/
32 /* Exported constants */
33 
34 /*
35  * types of data sources
36  */
37 #define GAUGE    1
38 #define COUNTER  2
39 #define DERIVE   3
40 #define ABSOLUTE 4
41 
42 /*
43  * consolidation functions
44  */
45 #define MIN 9
46 #define MAX 10
47 #define AVG 11 /* AVERAGE */
48 #define LST 12 /* LAST */
49 
50 /*
51  * predefined RRAs
52  */
53 #define RRA_MAX_7H_1M   CreateRRAStr ( MAX,       7,    1 )
54 #define RRA_AVG_7H_1M   CreateRRAStr ( AVG,       7,    1 )
55 #define RRA_MIN_7H_1M   CreateRRAStr ( MIN,       7,    1 )
56 #define RRA_MAX_25H_5M  CreateRRAStr ( MAX,      25,    5 )
57 #define RRA_AVG_25H_5M  CreateRRAStr ( AVG,      25,    5 )
58 #define RRA_MIN_25H_5M  CreateRRAStr ( MIN,      25,    5 )
59 #define RRA_MAX_8D_15M  CreateRRAStr ( MAX,    8*24,   15 )
60 #define RRA_AVG_8D_15M  CreateRRAStr ( AVG,    8*24,   15 )
61 #define RRA_MIN_8D_15M  CreateRRAStr ( MIN,    8*24,   15 )
62 #define RRA_MAX_32D_1H  CreateRRAStr ( MAX,   32*24,   60 )
63 #define RRA_AVG_32D_1H  CreateRRAStr ( AVG,   32*24,   60 )
64 #define RRA_MIN_32D_1H  CreateRRAStr ( MIN,   32*24,   60 )
65 #define RRA_MAX_367D_4H CreateRRAStr ( MAX,  367*24, 4*60 )
66 #define RRA_AVG_367D_4H CreateRRAStr ( AVG,  367*24, 4*60 )
67 #define RRA_MIN_367D_4H CreateRRAStr ( MIN,  367*24, 4*60 )
68 
69 /*
70  * predefined timings
71  */
72 #define PLOT_1H_10S   17
73 #define PLOT_6H_1M    18
74 #define PLOT_24H_5M   19
75 #define PLOT_7D_15M   20
76 #define PLOT_31D_60M  21
77 #define PLOT_366D_4H  22
78 
79 /*
80  * vertical sizes of plots
81  */
82 #define PLOT_H_UNIDIR 200
83 #define PLOT_H_BIDIR  320
84 
85 /*
86  * SI exponent names
87  */
88 #define PLOT_SI_AUTO   33
89 #define PLOT_SI_FEMTO  34
90 #define PLOT_SI_PICO   35
91 #define PLOT_SI_NANO   36
92 #define PLOT_SI_MICRO  37
93 #define PLOT_SI_MILLI  38
94 #define PLOT_SI_NONE   39
95 #define PLOT_SI_KILO   40
96 #define PLOT_SI_MEGA   41
97 #define PLOT_SI_GIGA   42
98 #define PLOT_SI_TERA   43
99 #define PLOT_SI_PETA   44
100 
101 /*
102  * Plot types
103  */
104 #define PLOT_AREA   65
105 #define PLOT_LINE1  66
106 #define PLOT_LINE2  67
107 #define PLOT_LINE3  68
108 #define PLOT_STACK  69
109 
110 /*
111  * Predefined colors
112  */
113 #define C_USAGE1_MAX "#ffbbbb"
114 #define C_USAGE1_AVG "#f83838"
115 #define C_USAGE2_MAX "#ffda9f"
116 #define C_USAGE2_AVG "#ff9900"
117 #define C_FREE_MAX   "#ccffcc"
118 #define C_FREE_AVG   "#3bf63b"
119 #define C_LOAD1_MAX  "#ff0000"
120 #define C_LOAD1_AVG  "#9f0000"
121 #define C_LOAD2_MAX  "#00ff00"
122 #define C_LOAD2_AVG  "#009f00"
123 #define C_LOAD3_MAX  "#0000ff"
124 #define C_LOAD3_AVG  "#00009f"
125 #define C_LOAD4_MAX  "#ffff00"
126 #define C_LOAD4_AVG  "#9f9f00"
127 #define C_THRU_MAX   "#ccccff"
128 #define C_THRU_AVG   "#3b3bf6"
129 #define C_ERR1_MAX   "#ffbbbb"
130 #define C_ERR1_AVG   "#f83838"
131 #define C_ERR2_MAX   "#ffda9f"
132 #define C_ERR2_AVG   "#ff9900"
133 #define C_THRES_MAX  "#ffbbbb"
134 #define C_THRES_AVG  "#f83838"
135 #define C_GAUGE1_MAX "#ccccff"
136 #define C_GAUGE1_AVG "#3b3bf6"
137 
138 
139 
140 /******************/
141 /* Exported types */
142 
143 /*
144  * File Names structure.
145  *
146  * Used to store various file names and pathes, handled by the FN* functions.
147  */
148 typedef struct
149 {
150     char* rrdHostDirPath;
151     char* rrdFilePath;
152     char* pngHostDirName;
153     char* pngFileName;
154     char* pngFilePath;
155     char* tmpPNGFilePath;
156 } tFN;
157 
158 /*
159  * Plot Parameters structure.
160  *
161  * Used to handle various parameters of a plot like unit, dimension and
162  * padding of texts. Handled by the PP* functions
163  */
164 typedef struct
165 {
166     char** dim;
167     char** unit;
168     int* stack;
169     int count;
170     int dimLen;
171     int unitLen;
172 } tPP;
173 
174 
175 /**********************/
176 /* Exported functions */
177 
178 /*
179  * Returns the range in seconds of the given timing.
180  */
181 int TimingGetRange ( int aTiming );
182 
183 /*
184  * Returns the sample interval in seconds of the given timing.
185  */
186 int TimingGetInterval ( int aTiming );
187 
188 /*
189  * Returns the width scale factor of the given timing.
190  */
191 float TimingGetXScale ( int aTiming );
192 
193 /*
194  * Returns the description string for the given timing. This string is
195  * newly malloced and should be freed by the caller.
196  */
197 char* TimingGetDescr ( int aTiming );
198 
199 /*
200  * Checks if a directory exists. If not then it tries to create it. If the
201  * directory neither existed nor could be created, then it exits the
202  * program.
203  */
204 void CheckDir ( const char* aDir );
205 
206 /*
207  * Builds a filename from the host directory and the globals srvName and
208  * srvId. If srvId is NULL, then the name is build without it. The function
209  * returns the file name in a newly malloced buffer which should be freed by
210  * the caller.
211  */
212 char* BuildRRDFilePath ( const char* aRRDHostDirPath );
213 
214 /*
215  * Checks if a rrd data file exists. If not then it tries to create it by
216  * calling CreateDataFile(). If it the database still does not exist, then
217  * it exits the program.
218  */
219 void CheckRRDDataFile ( const char* dataFile );
220 
221 /*
222  * Creates a string suitable as "DS" term for "rrd create".
223  *
224  * param aName name of the data source.
225  * param aType type ot the data source, see above and man 1 rrdcreate
226  * param aHBeat maximum time in seconds between samples.
227  * param aMin string containing the minimum for values of this DS
228  * param aMax string containing the maximum for values of this DS
229  *
230  * return pointer to a newly malloced string containing the term
231  *
232  * For more information about the parameters, see man 1 rrdcreate
233  */
234 char* CreateDSStr ( const char* aName, int aType, int aHBeat, char* aMin,
235         char* aMax );
236 
237 /*
238  * Creates a string suitable as "RRA" term for "rrd create".
239  *
240  * param aCF consolidation function
241  * param aRangeHours number of hours this RRA should keep
242  * param aCDPIntMinutes number of minutes between Consolidated Data Points
243  *
244  * return pointer to a newly malloced string containing the term
245  */
246 char* CreateRRAStr ( int aCF, int aRangeHours, int aCDPIntMinutes );
247 
248 /*
249  * Creates a rrd archive by calling rrd_create and evaluates the return value.
250  *
251  * param aParamCnt number of parameters in the aParams array.
252  * param aParams the params array.
253  * param aRRDFilePath the rrd file from which the plot is created.
254  */
255 void RRDCreate ( int aParamCnt, char** aParams, const char* aRRDFilePath );
256 
257 /*
258  * Updates a rrd archive -- that is: writes values to that archive -- by
259  * calling rrd_update and evaluates the return value.
260  *
261  * param aParamCnt number of parameters in the aParams array.
262  * param aParams the params array.
263  * param aRRDFilePath the rrd file from which the plot is created.
264  */
265 void RRDUpdate ( int aParamCnt, char** aParams, const char* aRRDFilePath );
266 
267 /*
268  * Initializes a tFN structure and returns a pointer to it. This structure has
269  * to be modified by FNSetPNGData() and can then be used to create a plot.
270  * Once finished, FNDone() should be called to free all resources used
271  * by the structure.
272  *
273  * return pointer to a newly malloced tFN structure.
274  */
275 tFN* FNInit ();
276 
277 /*
278  * Releases all resources allocated by the given tFN structure and the
279  * structure itself.
280  *
281  * param aFN Structure which should be deleted.
282  */
283 void FNDone ( tFN* aFN );
284 
285 /*
286  * Adds information about the plot file to the given tFN structure.
287  *
288  * param aFN Structure which should be modified.
289  * param aPlotId the plot id (e.g. "pkt" or "oct"), can be 0.
290  * param aTiming timing from which a description string is constructed.
291  */
292 void FNSetPNGData ( tFN* aFN, const char* aPlotId, int aTiming );
293 
294 /*
295  * Deletes information about a specific plot from the given tFN structure.
296  * This can be called after a plot file has been created. It will be called
297  * by FNSetPNGData() anyway.
298  */
299 void FNUnsetPNGData ( tFN* aFN );
300 
301 /*
302  * Initializes a tPP structure and returns a pointer to it. This structure has
303  * contains a set of data for each set of parameters which should be printed
304  * using PlotPrintParams(). Once finished, PPDone() should be called to free
305  * all resources used by the structure.
306  *
307  * return pointer to a newly malloced tPP structure.
308  */
309 tPP* PPInit ();
310 
311 /*
312  * Adds information about one type of data (roughly: one call to
313  * PlotPrintParams()). Note that "plots" must be be added in the same order
314  * the corresponding calls to PlotPrintParams() are made, otherwise the
315  * descriptions will get messed up!
316  *
317  * param aPP Structure which should be modified.
318  * param aDim Dimension of the data (e.g. "usage", "width", "errors/time")
319  * param aUnit Unit of the data (e.g. "%", "mm", "1/s" or "errors/s")
320  * param aStack TRUE if the plot is stacked, false otherwise. Note: This does
321  *         not make the plot stacked, it only changes the text of some output.
322  */
323 void PPAddPlot ( tPP* aPP, const char* aDim, const char* aUnit, int aStack );
324 
325 /*
326  * Releases all resources allocated by the given tPP structure and the
327  * structure itself.
328  *
329  * param aPP Structure which should be deleted.
330  */
331 void PPDone ( tPP* aPP );
332 
333 /*
334  * Sets up the standard parameters for plotting data from a RRD file.
335  * All strings are newly allocated and should be freed by the caller.
336  * If plotNewOutput is TRUE then the title and the start and end times
337  * are written to stdout.
338  *
339  * param array of strings in which the parameters are returned.
340  * param aFN tFN structure for this plot
341  * param aTitle title of the graph
342  * param aTiming timing given as one of the predefined timing constants.
343  * param aVLabel Y-Unit of the graph
344  * param aEnd Time at which the last plot should end. Should be a rrd
345  *         heartbeat ago.
346  * param aSIBase Base for SI Multipliers (1024 or 1000).
347  * param aSIExp SI Exponent as defined by "SI exponent names" above.
348  * param aLower lower limit.
349  * param aUpper upper limit. Upper and lower limit are only used if
350  *         aLower < aUpper. Otherwise rrdtool's autoconfiguration is used.
351  * param aUsingBidir not null if the plot uses negative number. Just to
352  *         make the plot larger.
353  *
354  * return the number of parameters assigned so far.
355  */
356 int PlotStdParams ( char** aParams, tFN* aFN, const char* aTitle,
357         const char* aVLabel, int aTiming, int aEnd, int aSIBase, int aSIExp,
358         int aLower, int aUpper, int aUsingBidir );
359 
360 /*
361  * Creates a definition for a virtual name of a data source.
362  *
363  * param aVName Virtual name of the data source.
364  * param aFN tFN structure for this plot
365  * param aDS The name of the data source.
366  * param aCF The consolidation function as given in rrdif.h.
367  *
368  * return pointer to a newly malloced string containing the term.
369  */
370 char* CreateDEF ( const char* aVName, tFN* aFN, const char* aDS,
371         int aCF );
372 
373 /*
374  * Creates a definition for a plot.
375  *
376  * param aType Type of the plot as defined in rrd.h
377  * param aVName Virtual name of the data source.
378  * param aColor Web-like color specification for the line. This can be NULL
379  *         in which case the plot will be blind and no legend will be printed.
380  * param aPP Plot parameters structure for this plot.
381  * param aPPI Index of the plot parameters starting at 0.
382  * param aCF The consolidation function as given in rrdif.h. This will be
383  *         printed right of the name (if that is not NULL).
384  * param aNL Insert a new line at the end of the legend. In that case the
385  *         legend will not be padded.
386  *
387  * return pointer to a newly malloced string containing the term.
388  */
389 char* CreatePlot ( int aType, const char* aVName, const char* aColor,
390         tPP* aPP, int aPPI, int aCF, int aNL );
391 
392 /*
393  * Creates the parameters for printing the average, minimum, maximum and last
394  * values either into the plot itself (if plotNewOutputFormat is FALSE) or
395  * to the results list (if plotNewOutputFormat is TRUE).
396  *
397  * Note: Padding will fail if the width of the format is too small.
398  *
399  * param aI Index of the first param to be set.
400  * param aParams the params array.
401  * param aPP Plot parameters structure for this plot.
402  * param aPPI Index of the plot parameters starting at 0.
403  * param aVNAvg Virtual name of the data source for the average value.
404  * param aVNMin Virtual name of the data source for the minimum value.
405  * param aVNMax Virtual name of the data source for the maximum value.
406  * param aVNLst Virtual name of the data source for the last value.
407  * param aFormat Format of the value (e.g. "%5.2lf").
408  *
409  * return Index of the next param to be set.
410  */
411 int PlotPrintParams ( int aI, char** aParams, tPP* aPP, int aPPI,
412         const char* aVNAvg, const char* aVNMin, const char* aVNMax,
413         const char* aVNLst, const char* aFormat );
414 
415 /*
416  * Graphs a RRD archive by calling rrd_graph and evaluating the return
417  * value and results array.
418  *
419  * param aParamCnt number of parameters in the aParams array.
420  * param aParams the params array.
421  * param aFN tFN structure for this plot.
422  * param aPP tPP structure for this plot.
423  */
424 void RRDGraph ( int aParamCnt, char** aParams, tFN* aFN, tPP* aPP );
425 
426 /*
427  * Logs the performance data.
428  */
429 void LogPerfData ();
430 
431 #endif /* WITH_RRD */
432 
433 #endif /* RRDIF_H */
434