File: rrd.php

package info (click to toggle)
jffnms 0.8.3dfsg.1-2.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 4,928 kB
  • ctags: 5,050
  • sloc: php: 19,981; sh: 266; makefile: 91; perl: 52
file content (90 lines) | stat: -rw-r--r-- 2,925 bytes parent folder | download | duplicates (5)
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
<?
/* This file is part of JFFNMS
 * Copyright (C) <2002-2005> Javier Szyszlican <javier@szysz.com>
 * This program is licensed under the GNU GPL, full terms in the LICENSE file
 */

function backend_rrd($options,$result) {

    $ret = -1;
    
    $filename = $GLOBALS["rrd_real_path"]."/interface-".$options["interface_id"].".rrd";

    $type_info = interface_get_type_info($options["interface_id"]);

    //RRD File Creation
    if ($type_info["rrd_mode"]==1)  //one file
	if (file_exists($filename)==FALSE) 
	    $ret = rrdtool_create_file($filename,$type_info["rrd_structure_def"],$type_info["rrd_structure_rra"],
		$type_info["rrd_structure_res"],$type_info["rrd_structure_step"]);

    if ($type_info["rrd_mode"]==2) { //multiple files
	$rrd_struct = explode (" ",$type_info["rrd_structure_def"]);
	foreach ($rrd_struct as $dsn=>$data) {
	    $filename_ds = str_replace(".rrd","-$dsn.rrd",$filename);
	    $aux = explode(":",$data);
	    $aux[1]="data";

	    if (file_exists($filename_ds)==FALSE)
		$ret = rrdtool_create_file($filename_ds,join(":",$aux),$type_info["rrd_structure_rra"],
		    $type_info["rrd_structure_res"],$type_info["rrd_structure_step"]);
	}
    }

    $dss = interface_parse_rrd_structure ($type_info["rrd_structure_def"]);
    $num_ds = count($dss);

    if ( $ret == 0 ) {
        $err = rrdtool_error();
        logger( "RRD error: $err",0);
	return 0;
    } else { 
	// RRD File update 
	
	$output = array();
	$rrd_options[0] = "N";
	
	if ($options["backend_parameters"]=="*") { //ALL DS's
	    $poller_buffer = $GLOBALS["session_vars"]["poller_buffer"]; //get data from the poller buffer 

	    foreach ($dss as $ds_name=>$ds_number) { 
		$buffer_name = "$ds_name-".$options["interface_id"];
		if (isset($poller_buffer[$buffer_name])) 
		    $rrd_options[$ds_number+1] = $poller_buffer[$buffer_name];
	    }
	} else { //only one DS
	    if ($type_info["rrd_mode"]==1) 	//all DS's on the same file
		sleep(1);			//to avoid errors

	    $rrd_options[$dss[$options["poller_name"]]+1] = $result;
	}

	for ($i = 1; $i < $num_ds+1; $i++) 
	    if (!isset($rrd_options[$i]) || !is_numeric($rrd_options[$i])) //if its not valid
		$rrd_options[$i]="U"; //Put in NaN

	if ($type_info["rrd_mode"]==1) { //all DS's on the same file
	    $ret = rrdtool_update($filename,join (":",$rrd_options));
	    $output[] = "ALL=".join (":",$rrd_options);
	}

	if ($type_info["rrd_mode"]==2) { //only one DS by file
	    $ds_names = array_keys($dss);
	    
	    for ($dsn = 0; $dsn < $num_ds; $dsn++)
		if (is_numeric($rrd_options[$dsn+1])) { //if there is a value 
	    	    $filename_ds = str_replace(".rrd","-$dsn.rrd",$filename);
		    $ret = rrdtool_update($filename_ds,$rrd_options[0].":".$rrd_options[$dsn+1]);
		    $output[] = $ds_names[$dsn].":".$rrd_options[$dsn+1];
		}

	    unset ($ds_names);
	}
	
	if ( $ret == 0 ) logger( "RRD error: ".rrdtool_error(),0);

	return join(" - ",$output);
    }
}

?>