<commit_before>package Core::Uptime;

use strict;
use warnings;

use base 'Resmon::Module';

use Resmon::ExtComm qw(run_command);

=pod

=head1 NAME

Core::Uptime - track system boot time and uptime 

=head1 SYNOPSIS

 Core::Uptime {
     uptime: noop
 }

=head1 DESCRIPTION

This module monitors system uptime and boot time

=head1 CONFIGURATION

=over

=item check_name

The check name is descriptive only in this check. It is not used for anything.

=back

=head1 METRICS

=over

=item boottime 

The time when the system booted

=item uptime 

The number of seconds between when the system last booted and when the check ran

=back

=cut

sub handler {
    my $self = shift;
    my $config = $self->{config}; # All configuration is in here
    my $kstat_path = $self->{kstat_path} || 'kstat';

    my $current_time = time(); 
    my $uptime;
    my $boottime;

    if ( $^O eq "solaris" ) {
       my $zone = 0;

       my $check_006 = run_command("grep r151006 /etc/release");
       chomp($check_006);
       my $zonename = run_command("/usr/bin/zonename");
       chomp $zonename;

       #Zones on r151006 do not report boot time from kstat correctly
       if (($check_006 ne "") && ($zonename ne "global")) {
          $uptime = run_command('ptime -Fp `pgrep init` 2>&1 | grep real | awk \'{print $2}\' | awk -F: \'{print ($1*3600) + ($2*60) + $3}\'');
          chomp $uptime;
          $boottime = $current_time - $uptime;
       } else {  
          my $output = run_command("$kstat_path -p unix:0:system_misc:boot_time");
          chomp $output;

          ($boottime) =
             $output =~ /^unix:0:system_misc:boot_time\s+(\d+)$/;
          $uptime =
              $current_time - $boottime;
      }
    } else {
       $uptime = run_command("cat /proc/uptime | awk '{print $1}' | cut -d . -f 1");<commit_msg>Use file handles instead of system commands<commit_after>package Core::Uptime;

use strict;
use warnings;

use base 'Resmon::Module';

use Resmon::ExtComm qw(run_command);

=pod

=head1 NAME

Core::Uptime - track system boot time and uptime 

=head1 SYNOPSIS

 Core::Uptime {
     uptime: noop
 }

=head1 DESCRIPTION

This module monitors system uptime and boot time

=head1 CONFIGURATION

=over

=item check_name

The check name is descriptive only in this check. It is not used for anything.

=back

=head1 METRICS

=over

=item boottime 

The time when the system booted

=item uptime 

The number of seconds between when the system last booted and when the check ran

=back

=cut

sub handler {
    my $self = shift;
    my $config = $self->{config}; # All configuration is in here
    my $kstat_path = $self->{kstat_path} || 'kstat';

    my $current_time = time(); 
    my $uptime;
    my $boottime;

    if ( $^O eq "solaris" ) {
       my $check_006;
       open my $release_fh,'<',"/etc/release" or die "Could not find /etc/release";
       while (my $line = <$release_fh>) {
          if ($line =~ /r151006/) {
             $check_006 = $line;
             chomp($check_006);
          }
       }
       close $release_fh;

       my $zonename = run_command("/usr/bin/zonename");
       chomp $zonename;

       #Zones on r151006 do not report boot time from kstat correctly
       if (($check_006 ne "") && ($zonename ne "global")) {
          $uptime = run_command('ptime -Fp `pgrep init` 2>&1 | grep real | awk \'{print $2}\' | awk -F: \'{print ($1*3600) + ($2*60) + $3}\'');
          chomp $uptime;
          $boottime = $current_time - $uptime;
       } else {  
          my $output = run_command("$kstat_path -p unix:0:system_misc:boot_time");
          chomp $output;

          ($boottime) =
             $output =~ /^unix:0:system_misc:boot_time\s+(\d+)$/;
          $uptime = $current_time - $boottime;
      }
    } else {
       open my $uptime_fh,'<',"/proc/uptime" or die "Could not find /proc/uptime";
       while (my $line = <$uptime_fh>) {
           if ($line =~ /^(\d+)\.\d+\s+\d+\.\d+$/) {
              $uptime = $1;
           }
       }
       close $uptime_fh;
{'fraction_non_alphanumeric': 0.13632977402532903, 'fraction_numerical': 0.021604171840079463, 'mean_word_length': 2.472413793103448, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '611990', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1390, 'n_words': 417}