Parse Linux Uptime


/ Published in: Perl
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/perl
  2. #
  3. # Author : Keiran "Affix" Smith <Affix_at_Affix_dot_me>
  4. # Website: http://keiran-smith.net
  5. # Description :
  6. # Parse the output of the linux uptime command into easy to understand
  7. # readable text x Days y Hours z Minutes
  8. # UPDATES :
  9. # - December 19th 2010
  10. # + Fixed a bug regarding minutes
  11. #
  12. # This program is free software; you can redistribute it and/or modify
  13. # it under the terms of the GNU General Public License as published by
  14. # the Free Software Foundation; version 2 of the License.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20.  
  21. # Get and Parse System Uptime
  22. @uptime1 = split(/\,/, `uptime`);
  23. @uptime2 = split(/ /, $uptime1[0]);
  24. @uptime2 = split(/\:/, $uptime2[3]);
  25.  
  26. @hrmin = split(/ /, $uptime1[0]);
  27.  
  28. @days = split(/ /, $uptime1[0]);
  29.  
  30. $days[3] =~ s/[0-9][0-9]:[0-9][0-9]/0/;
  31.  
  32. if($days[3] < 1)
  33. {
  34. @hrmins = split(/\:/, $hrmin[4]);
  35. $uptime = $hrmins[0] . " Hours " . $hrmins[1] . " Minutes";
  36. }
  37. else
  38. {
  39. $uptime1[1] =~ s/ //;
  40. @hrmins = split(/\:/, $uptime1[1]);
  41. @hours = split(/ /, $hrmins[0]);
  42. if($hours[2] == 'users')
  43. {
  44. $uptime = $days[3] . " Minutes";
  45. }
  46. else
  47. {
  48. $uptime = $days[3] . " Days " . $hrmins[0] . " Hours " . $hrmins[1] . " Minutes";
  49. }
  50. }
  51. print $uptime;

URL: http://affix.me

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.