Question

first let me apologize, I'm a Network engineer and not a coder... so please if you will bear with me here.

Here's what I'm up against and I can't for the life of me find an elegant way to do it.

I'm using nagios (sure many of you are familiar with it) and am getting performance data from the service checks. This one in particular returns values like: module 2 inlet temperature module 2 outlet temperature module 2 asic-4 temperature module 3 inlet temperature module 3 outlet temperature module 4 inlet temperature module 4 outlet temperature ... and so on These values are all presented in a single array. What I'm trying to do is: match the first 2 words/values in the string in order to create "groups" of array key values to be used to generate RRD graphs with... the RRD part I don't need any help with, but the matching and output I do.

I should also note that there may be different array values in here as well depending on the device the data is coming from (i.e. it might show up as "Switch #1 Sensor #1 Temperature") and while I'm not worried about that for the moment, I will be using this script to evaluate those values in the future to create their own respective graphs.

So, down to business, my thought was to create two arrays from the original: use preg_match initially to look for /.outlet.|.asic./ as these are "hot" temps, then further refine by breaking that new array down to either just the second value (int) or first two values (module #) for later comparison

use preg_match secondly to look for /.inlet./ as these are "cold" temps, then further refine by breaking that new array down same as the former.

Should now have two arrays with either key=># or key=>module # then use array_intersect to look for matches across the two arrays and output the keys so I can use them to generate the graphs.

Does that make sense? In other words, I only want matching module # entries to be selected to use in my graphing. i.e. module 2 inlet, module 2 outlet, module 2 asic... then repeat for - module 3 inlet, module 3 outlet, etc...

This is what I've tried and it hasn't worked the way I wanted at all:

$test = array("module 1 inlet temperature", "module 2 inlet temperature", "module 2 asic-4 temperature", "module 2 outlet temperature", "module 1 outlet temperature");
$results = array();
foreach($test as $key => $value) {
   preg_match("/.*inlet.*|.*asic.*/", $test[$key]);
   preg_match("/module [0-9]?[0-9]/", $test[$key]);      
   $results[] = $value;
   }

if(preg_match("/.*outlet.*/", $test[$key]));
   foreach($test as $key1 => $value1) {
      preg_match("/module [0-9]?[0-9]/", $test[$key1]);
   $results1[] = $value1;
   }#
}
$results3 = array_intersect($results, $results1)

Any help here would be really appreciated. I'm sure my explanation here was pretty confusing so hope someone takes pity on me and gives a guy a hand...

Thanks in advance.

Was it helpful?

Solution

It's a little hard to understand your question, but I'm imagining you're after results like these?

$temps['module 1']['inlet'] = 20;
$temps['module 1']['outlet'] = 30;

$temps['module 2']['inlet'] = 25;
$temps['module 2']['outlet'] = 35;
$temps['module 2']['asic-4'] = 50;

You will then use these arrays to generate your graphs?

As long as you had the labels in one array, and the temp values in another, and the order of labels and temps are identical in each array... then this is how you would do it:

// Split Names into Groups
$temps = array(20,25,50,35,30);
$labels = array("module 1 inlet temperature", "module 2 inlet temperature", "module 2 asic-4 temperature", "module 2 outlet temperature", "module 1 outlet temperature");

// Combine Lables to Values (Labels and Values must be in the same positions)
$data = array_combine($labels, $temps);

$temps = array();
foreach ($data as $label => $temp) {
    $words = preg_split('/\s/i', $label);

    // Combine first two pieces of label for component name
    $component = $words[0] . ' ' . $words[1];

    // Sensor name is on it's own
    $sensor = $words[2];

    // Save Results
    $temps[$component][$sensor] = $temp;
}

// Print out results for debug purposes
echo '<pre>';
var_dump($temps);
echo '</pre>';
exit();

Once you have the $temp array, you can use foreach loops to run through each module and sensor and print out the values for your graphs, or only show certain modules, or certain sensors etc.

Even if it isn't exactely what you're after, hopefully it'll give you some ideas and you can tweak it to suit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top