Grab line from text file

Hello,

I'm using the code attached to grab cpu usage at the top of each hour.  I need to now grab the cpu usage every five minutes.

I do not know reg ex and this is the primary driver for this script, I believe this expression is what is used to grab at the top of every hour:
preg_match('/\W(\d+)%/', $fileText, $matches);

Here is an example of the log file we're using:
[Mon Jan 11 00:36:48.157 2010] [22:36:57: 43] cpu
[Mon Jan 11 00:36:48.157 2010] CPU Load : 86%
[Mon Jan 11 00:41:47.630 2010]
[Mon Jan 11 00:41:47.630 2010]
[Mon Jan 11 00:41:48.504 2010] [22:41:57: 48] cpu
[Mon Jan 11 00:41:48.504 2010] CPU Load : 92%
[Mon Jan 11 00:41:50.111 2010] eth0: TxQueue Saturated! [12/139 packets dropped]
[Mon Jan 11 00:42:06.085 2010]
[Mon Jan 11 00:42:06.085 2010]
[Mon Jan 11 00:42:06.740 2010] [22:42:15:534] spoof sessions
[Mon Jan 11 00:42:06.740 2010] Sessions  : 0
[Mon Jan 11 00:42:06.740 2010] Num Sessions Ever Created: 0
[Mon Jan 11 00:42:06.740 2010] Num Sessions Ever Destroyed: 0
[Mon Jan 11 00:46:47.618 2010]
[Mon Jan 11 00:46:47.618 2010]
[Mon Jan 11 00:46:48.196 2010] [22:46:57: 66] cpu
[Mon Jan 11 00:46:48.196 2010] CPU Load : 92%
[Mon Jan 11 00:50:22.150 2010] eth0: TxQueue Saturated! [21/186 packets dropped]
[Mon Jan 11 00:51:47.606 2010]
[Mon Jan 11 00:51:47.606 2010]
[Mon Jan 11 00:51:48.168 2010] [22:51:57: 43] cpu
[Mon Jan 11 00:51:48.168 2010] CPU Load : 87%
[Mon Jan 11 00:51:52.162 2010] eth0: TxQueue Saturated! [7/197 packets dropped]
[Mon Jan 11 00:51:54.174 2010] eth0: TxQueue Saturated! [11/275 packets dropped]
[Mon Jan 11 00:51:55.172 2010] eth0: TxQueue Saturated! [50/218 packets dropped]
[Mon Jan 11 00:51:56.327 2010] eth0: TxQueue Saturated! [53/190 packets dropped]
[Mon Jan 11 00:51:57.107 2010] eth0: TxQueue Saturated! [27/153 packets dropped]
[Mon Jan 11 00:52:06.186 2010]
[Mon Jan 11 00:52:06.186 2010]
[Mon Jan 11 00:52:06.826 2010] [22:52:15:640] spoof sessions
[Mon Jan 11 00:52:06.826 2010] Sessions  : 0
[Mon Jan 11 00:52:06.826 2010] Num Sessions Ever Created: 0
[Mon Jan 11 00:52:06.826 2010] Num Sessions Ever Destroyed: 0
[Mon Jan 11 00:52:07.465 2010] eth0: TxQueue Saturated! [29/200 packets dropped]
[Mon Jan 11 00:52:12.098 2010] eth0: TxQueue Saturated! [48/216 packets dropped]
[Mon Jan 11 00:52:15.172 2010] eth0: TxQueue Saturated! [38/166 packets dropped]
[Mon Jan 11 00:52:16.232 2010] eth0: TxQueue Saturated! [64/172 packets dropped]
[Mon Jan 11 00:52:18.416 2010] eth0: TxQueue Saturated! [39/143 packets dropped]
[Mon Jan 11 00:52:19.072 2010] eth0: TxQueue Saturated! [38/195 packets dropped]
[Mon Jan 11 00:56:47.189 2010] eth0: TxQueue Saturated! [35/223 packets dropped]
[Mon Jan 11 00:56:47.797 2010]
[Mon Jan 11 00:56:47.797 2010]
[Mon Jan 11 00:56:47.797 2010] [22:56:57: 66] cpu
[Mon Jan 11 00:56:48.437 2010] CPU Load : 77%
$hour = array(); 
$month = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4,  "May" => 5,  "Jun" => 6,
               "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12 );

$file = fopen("uploads/" . basename( $_FILES['uploadedfile']['name']), "r") or exit("Unable to open file!"); 
while(!feof($file)) { 
     $fileText = fgets($file); 
     preg_match('/\W(\d+)%/', $fileText, $matches);
     if($matches) {
          $cpuLoad[] = $matches[1]; 
          
     	  // Pull out hours and minutes
          //
          preg_match('/([A-Za-z]{3})\s([a-zA-Z]{3})\s(\d{2})\s(\d{2})([\.:0-9]+)\s.*/', $fileText, $match );
          if ( $match ) {
          
               // See if this hour is in the hour table. If not then add it
               // and record the CPU number
               //
               $intMonth = intval( $month[$match[2]] );
               $intDay = intval($match[3]);
               $intHour = intval($match[4]);
               $index  = $intMonth * 10000 + $intDay*100 + $intHour;
               if ( !array_key_exists( $index, $hour )  ) {
                    $cpuTime = $match[1] . $match[4];
                    $hour [$index]['cpu'] = $matches[1];
                    $hour [$index]['time'] = $match[4] . $match[5];
                    $hour [$index]['day'] = $match[1];
                    $hour [$index]['month'] = $match[2];
               }     
          }   
              
     } 
}

Open in new window

vrmetroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
The pattern:

/\W(\d+)%/

is made up as follows:

The / at the start and end are pattern terminators.
\W means a single character that's not alphabetic or numeric or an underscore.
\d+ means at least one but as-many-as-possible numbers
% is looking for a percent character
The ( and ) say to grab all the numbers before the percent characters as the result you're after.

So you're just getting the number before a % character.

It's the bit of code under the heading:
// Pull out hours and minutes

that's checking whether or not to keep the CPU number. More to come in my next post.
0
vrmetroAuthor Commented:
ok because I'm using this to check and make sure there is a value, makes sense

if(!$cpuLoad){
      exit("Sorry, your file does not meet the CPU Usage log file format<br/>");
}

so to grab all the cpu usage strings not simply the ones at the top of each hour i need to adjust what?

0
Terry WoodsIT GuruCommented:
This might do the trick.

$hour is now $cpuResults as the "hour" name no longer has accurate meaning

Note that the indexing has been changed to allow minutes as well as hours. This might have further implications when accessing the results.

$cpuTime has a minute value added, in case it matters.
$cpuResults = array(); 
$month = array("Jan" => 1, "Feb" => 2, "Mar" => 3, "Apr" => 4,  "May" => 5,  "Jun" => 6,
               "Jul" => 7, "Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12 );

$file = fopen("uploads/" . basename( $_FILES['uploadedfile']['name']), "r") or exit("Unable to open file!"); 
while(!feof($file)) {
     preg_match('/\W(\d+)%/', $fileText, $matches);
     if($matches) {
          $cpuLoad[] = $matches[1]; 
          
          // Pull out hours and minutes
          //
          preg_match('/([A-Za-z]{3})\s([a-zA-Z]{3})\s(\d{2})\s(\d{2}):(\d{2})([\.:0-9]+)\s.*/', $fileText, $match );
          if ( $match ) {
          
               // record the CPU number
               //
               $intMonth = intval( $month[$match[2]] );
               $intDay = intval($match[3]);
               $intHour = intval($match[4]);
               $intMinute = intval($match[5]);
               $index  = $intMonth * 1000000 + $intDay*10000 + $intHour*100 + $intMinute; #Multiplied by 100 to fit minute value in
               $cpuTime = $match[1] . $match[4] . $match[5];
               $cpuResults[$index]['cpu'] = $matches[1];
               $cpuResults[$index]['time'] = $match[4] . $match[5].$match[6];
               $cpuResults[$index]['day'] = $match[1];
               $cpuResults[$index]['month'] = $match[2];
          }   
              
     } 
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Terry WoodsIT GuruCommented:
Your format check will succeed if there's any number with a percent to the right of it in the file, which is a bit dicy.

You're probably better to use this check:
if (count($cpuResults)==0) {
      exit("Sorry, your file does not meet the CPU Usage log file format<br/>");
}
0
vrmetroAuthor Commented:
Had to add back $fileText = fgets($file); but it appears to work, thank you!
0
vrmetroAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.