SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: preg_match help

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    USA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match help

    Ok, looking to do some trending on my CPU server loading so I need to obtain the values from the uptime output.

    Code:
    $up = exec("/usr/bin/uptime");
    printf("%s <BR>", $up);
    produces
    Code:
    9:40am up 13 days, 3:17, 1 user, load average: 2.15, 1.78, 1.45
    I'm interested in obtaining the values 2.15, 1.78 & 1.45 for writing to a file along with a time stamp. I can get this through several explode commands but there is probably a more eloquent way to do this with preg_match function.

    I just don't understand the string pattern syntax.

  2. #2
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if all you need is the last 3 values

    PHP Code:
        $string "9:40am up 13 days, 3:17, 1 user, load average: 2.15, 1.78, 1.45";
        
    preg_match('/(\d+\.\d+), (\d+\.\d+), (\d+\.\d+)$/'$string$matches);
        
    print_r($matches); 

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $up '9:40am up 13 days, 3:17, 1 user, load average: 2.15, 1.78, 1.45';
    preg_match_all('/\d\.\d+/'$up$matches);

    foreach (
    $matches[0] as $m)
    {
        echo 
    $m.'<br />';

    For the timestamp, just echo time() to the file at the same that you write the other values.

  4. #4
    SitePoint Enthusiast HogMan's Avatar
    Join Date
    Mar 2003
    Location
    Nebraska
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might be kind of messy...

    PHP Code:
    #!/usr/bin/php -q
    <?php

    //  10:54am  up 30 days,  3:11,  2 users,  load average: 0.00, 0.02, 0.00

    $uptime '/usr/bin/uptime';
    $result exec($uptime);
    $matches = array();

    preg_match('/average:\s([0-9]\.[0-9][0-9]),\s([0-9]\.[0-9][0-9]),\s([0-9]\.[0-9][0-9])/'$result$matches);

    echo 
    'Time 1: ' $matches[1] . "\n";
    echo 
    'Time 2: ' $matches[2] . "\n";
    echo 
    'Time 3: ' $matches[3] . "\n";
    ?>
    That will give you the last three, for timestamp you could parse that out of the line also or just get it via php function.

    Edit: After looking at it more, I noticed none of the other numbers have a decimal value in them, so it could be toned down a bit and only match those. I see someone posted that already

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about:
    PHP Code:
    var_dump(split(',',exec('uptime | cut -d : -f 4'))); 
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TMTOWTDI.

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    USA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    preg_match('/(\d+\.\d+), (\d+\.\d+), (\d+\.\d+)$/', $string, $matches);
    This worked like a charm..and yes I did echo time() to an INT field versus parsing the first pattern.

    I also have it recording the number of reg.users and guests visiting my forum and running from a cron job every 5 minutes.

    Now I just need a grab the data from the database and plot load, users as a function of time using jpGraph. I'm just learning this package..anyone have a quick example of how to do this?

    If I plot load as a function of users I can test for direct relationship. This is the only site on the server so I can directly test dependencies and test improvements.

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Boss Hoss
    Now I just need a grab the data from the database and plot load, users as a function of time using jpGraph. I'm just learning this package..anyone have a quick example of how to do this?
    Take a look at the two JpGraph links in my sig on Post #5


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •