?
Solved

Grab line from text file

Posted on 2010-01-12
6
Medium Priority
?
379 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:vrmetro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 26297074
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
 

Author Comment

by:vrmetro
ID: 26297303
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
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 total points
ID: 26297310
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 35

Expert Comment

by:Terry Woods
ID: 26297409
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
 

Author Comment

by:vrmetro
ID: 26297972
Had to add back $fileText = fgets($file); but it appears to work, thank you!
0
 

Author Closing Comment

by:vrmetro
ID: 31676318
Thank you
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question