Php - "first 2 words" for multiple matches for values ​​in an array, then array_intersect?

first let me apologize, i'm a network engineer, not an encoder ... so please if you carry with me here.

This is what I am opposed to, and I cannot find an elegant way for me to do this.

I use nagios (of course, many of you are familiar with it), and I get performance data from a service check. This, in particular, returns values ​​such as: temperature at the input of module 2 temperature at the output of module 2 temperature of the module 2-a-4 temperature at the entrance to module 3 temperature at the output of module 3 input temperature of the module 4 temperature at the output of module 4 .. . and so on. These values ​​are presented in one array. What I'm trying to do is: match the first two words / values ​​in a string to create “groups” of array key values ​​that will be used to generate RRD plots with ... part of RRD I don't need help, but I’m matching and finding to do.

It should also be noted that there may be different values ​​of the array, depending on the device from which the data comes from (ie it can be displayed as "Temperature No. 1 of sensor No. 1"), and for now I do not have to worry about it at the moment , I will use this script to evaluate these values ​​in the future to create my own graphs.

So, down to business, I was thinking of creating two arrays from the original: first use preg_match to search for /.outlet.|.asic./, since these are hot temps, and then clarify by breaking this new array into just the second value (int) or first two values ​​(module #) for later comparison

use preg_match secondly to search for /.inlet./, since this is a "cold" pace, and then refine by breaking up this new array in the same way as the first.

Now you need to have two arrays with the key => # or key => module # then use array_intersect to find matches between the two arrays and output the keys so that I can use them to generate graphs.

It makes sense? In other words, I want the appropriate entries in module # to be selected for input in my graph. that is, the input of module 2, release 2 of the module, module 2, and then repeat the input for module 3, the output of module 3, etc.

This is what I tried and it didn't work the way I wanted:

$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 am sure that my explanation here was rather confusing, so I hope that someone will pity me and give the guy a hand ...

Thanks in advance.

+4
source share
1 answer

It’s a little difficult to understand your question, but I imagine that you are after such results?

 $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; 

Then will you use these arrays to generate your graphs?

As long as you have labels in one array and temp values ​​in another, and the order of labels and rates is the same in each array ... then you will do this:

 // 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 own $sensor = $words[2]; // Save Results $temps[$component][$sensor] = $temp; } // Print out results for debug purposes echo '<pre>'; var_dump($temps); echo '</pre>'; exit(); 

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

Even if this is not exactly what you need, I hope it gives you some ideas, and you can customize it to fit your needs.

+1
source

All Articles