How to Create a Script to Parse a CSV in a Certain Format

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

Post Reply
state0fmind
Posts: 2
Joined: Sun Mar 04, 2012 7:00 pm

How to Create a Script to Parse a CSV in a Certain Format

Post by state0fmind »

Hi All,

I'm working on creating a script and corresponding Script Query XML file to take data in from CSV files.

Data is getting logged to CSV files on multiple servers and is pushed to the cacti server. The script I have then goes through all of the CSV files and outputs the data like so:

Code: Select all

ip:127.0.01 status:up timestamp:1330905982
ip:127.0.02 status:up timestamp:1330905982
ip:127.0.03 status:down timestamp:1330905982
ip:127.0.04 status:up timestamp:1330905982
I would like to be able to log each IP and its up time to individual graphs based on this. (Y axis of the graph is up/down, X axis of the graph is time)

I'm new to cacti and am not sure how to write the Script Query XML file, and Graph Template to make this work.

Thanks in advance for your help!
noname
Cacti Guru User
Posts: 1566
Joined: Thu Aug 05, 2010 2:04 am
Location: Japan

Re: How to Create a Script to Parse a CSV in a Certain Forma

Post by noname »

For example..

test.dat

Code: Select all

ip:127.0.0.1 status:up timestamp:1330905982
ip:127.0.0.2 status:up timestamp:1330905982
ip:127.0.0.3 status:down timestamp:1330905982
ip:127.0.0.4 status:up timestamp:1330905982
test.php

Code: Select all

<?php

$polling_interval = 300;

$target_ip = $_SERVER["argv"][1];

$now = time();
# $now = 1330906200;	// for debugging
$updown = array();

$lines = explode("\n", trim(file_get_contents("test.dat")));

foreach ($lines as $line) {
        list($t_ip, $t_status, $t_timestamp) = explode(" ", $line);
        list($tmp, $ip) = explode(":", $t_ip);
        list($tmp, $status) = explode(":", $t_status);
        list($tmp, $timestamp) = explode(":", $t_timestamp);

        if (($now - $polling_interval < $timestamp) && ($timestamp <= $now)) {
                $updown[$ip] = ($status == "up" ? 1 : 0);
        }
}

# print_r($updown);     // for debugging

if (isset($updown[$target_ip])) {
        echo $updown[$target_ip];
} else {
        echo "0";
}

?>
If "test.dat" contains valid data within the 5 minutes (=$polling_interval) before now, the above script returns the status of target IP.
(Otherwise it returns 0.)

% php test.php 127.0.0.1
1
% php test.php 127.0.0.3
0
% php test.php 127.0.0.99
0

Then you may try to create 'Data Input Method' to make graphs from the result.
Good luck.
- Walkthrough: My First Data Input Method - Cacti Docs
state0fmind
Posts: 2
Joined: Sun Mar 04, 2012 7:00 pm

Re: How to Create a Script to Parse a CSV in a Certain Forma

Post by state0fmind »

This is great! I will be working to implement it today/tomorrow then let you know how it goes.

Thanks!!!!!!!!!!!!
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests