Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Grab line from text file

Posted on 2010-01-12
6
Medium Priority
?
380 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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

577 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