Changeset 599


Ignore:
Timestamp:
Jun 27, 2011, 4:18:39 PM (10 years ago)
Author:
lucas
Message:

ec2-loadavg:

  • daemonize
  • cleanup comments and code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • cloudtools/ec2-loadavg

    r598 r599  
    1111use IO::File;
    1212use Time::localtime;
     13use POSIX 'setsid';
    1314
     15# Options
    1416my $threshold = 0.1;
    1517my $logfile = "/var/log/ec2-loadavg.log";
     18
     19# Internal variables
    1620my $fh = IO::File->new();
    17 
    1821my $start_date = time;
    1922my $tm = localtime($start_date);
     
    2427my $load;
    2528my $index;
    26 
    2729my @values;
    2830
    29 # Startup
     31sub daemonize;
     32
     33# open logs before daemonizing, to have a meaningful error message
    3034$fh->open(">> $logfile") or die "Error, cannot create logfile $logfile : $!";
     35
     36# detach from console to run in the background
     37daemonize();
     38
    3139printf $fh "[%02d/%02d/%04d:%02d:%02d:%02d] Starting with threshold %.2f\n",
    3240  $tm->mday, $tm->mon+1, $tm->year+1900,
     
    4048  $loadavg      = read_file("/proc/loadavg");
    4149
    42   # minutes is the second field
    4350  $min = $cur_date->min;
    4451  if ($loadavg =~ m/^(\d+\.?\d?)/) {
     
    4754
    4855    $values[$index] = $load;
    49 
    50     # print Dumper(\@values);
    5156  }
    5257 
    5358  # do we have 1 hour of measures?
    5459  if ($current_date - $start_date > 3600) {
    55     print "We have enough measures\n";
    5660    my $sum = 0;
    5761    foreach my $elem (@values) {
     
    5963    }
    6064
    61     printf $fh "[%02d/%02d/%04d:%02d:%02d:%02d] Collected measures over the last 60 minutes: loadavgsum is %.2f\n",
     65    printf $fh "[%02d/%02d/%04d:%02d:%02d:%02d] Collected measures over the last 60 minutes: loadavg sum is %.2f\n",
    6266      $tm->mday, $tm->mon+1, $tm->year+1900,
    6367      $tm->hour, $tm->min, $tm->sec, $sum;
     
    6973        $tm->hour, $tm->min, $tm->sec, $threshold;
    7074      $fh->flush;
     75
     76      # cleanup and shutdown
    7177      $fh->close;
    72 
    7378      system("halt");
    7479    }
     
    7883  sleep 60 * 5;
    7984}
     85
     86# taken from http://perldoc.perl.org/perlipc.html
     87sub daemonize {
     88  print "Starting daemon...\n";
     89  chdir("/")                  || die "can't chdir to /: $!";
     90  open(STDIN, "< /dev/null")  || die "can't read /dev/null: $!";
     91  open(STDOUT, "> /dev/null") || die "can't write to /dev/null: $!";
     92  defined(my $pid = fork())   || die "can't fork: $!";
     93 
     94  # non-zero now means I am the parent, cleanup and exit
     95  if ($pid) {
     96    $fh->close;
     97    exit
     98  }
     99
     100  (setsid() != -1)         || die "Can't start a new session: $!";
     101  open(STDERR, ">&STDOUT") || die "can't dup stdout: $!";
     102}
Note: See TracChangeset for help on using the changeset viewer.