Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 9.05 KB | None | 0 0
  1. package Report;
  2.  
  3. ### Author : Joshua S. Day (haxmeister)
  4. ### purpose : functions for retrieving data on funtoo linux
  5.  
  6. use strict;
  7. use warnings;
  8. use Exporter;
  9. use JSON;
  10. our $VERSION = '1.1';
  11.  
  12. our @EXPORT_OK = qw(user_config
  13.                     get_cpu_info
  14.                     get_mem_info
  15.                     get_kernel_info
  16.                     get_boot_dir_info
  17.                     get_version_info
  18.                     get_world_info
  19.                     get_profile_info
  20.                     get_kit_info);
  21.  
  22. sub user_config {
  23.  
  24.     my $config_file = '/etc/report.conf';
  25.     my %hash;
  26.  
  27.     if ( open( my $fh, '<:encoding(UTF-8)', $config_file ) ) {
  28.         my @lines = <$fh>;
  29.         close $fh;
  30.         foreach my $line (@lines) {
  31.             chomp $line;
  32.             if ( $line =~ /^\#/msx ) {
  33.                 next;
  34.             }
  35.             elsif ($line) {
  36.                 my ( $key, $value ) = split /\s*:\s*/msx, $line;
  37.                 $hash{$key} = $value;
  38.             }
  39.             else {
  40.                 next;
  41.             }
  42.         }
  43.     }
  44.     else {
  45.         warn "Could not open file ' $config_file' $!";
  46.         exit;
  47.     }
  48.     return %hash;
  49. }
  50.  
  51. ###
  52. ### fetching active profiles
  53. ### reconst output of epro show-json command
  54. ###
  55. sub get_profile_info {
  56.     my $json_from_epro = `epro show-json`;
  57.     my %profiles;
  58.     my $data = decode_json($json_from_epro);
  59.     %profiles = %$data;
  60.     return %profiles;
  61. }
  62.  
  63. ###
  64. ### fetching active kits
  65. ### resorting to parsing output of ego
  66. ###
  67. sub get_kit_info {
  68.     my @status_info = `ego kit status`;
  69.     my %hash;
  70.    
  71.     for my $line (@status_info){
  72.         chomp $line;
  73.         $line =~ s/^\s+|\s+$//g;
  74.        
  75.         if ( $line =~ /NOTE/){
  76.             return %hash;
  77.         }
  78.        
  79.         if ( $line =~ /^\w/msx){
  80.             my ($key, $value) = split(' ',$line);
  81.             $value =~ s/^\W\[\d.m//;
  82.             $hash{$key} = $value;
  83.            
  84.         }    
  85.     }
  86. }
  87.  
  88. ###
  89. ### fetching lines from /proc/cpuinfo
  90. ###
  91. sub get_cpu_info {
  92.  
  93.     my $cpu_file = '/proc/cpuinfo';
  94.     my %hash;
  95.     my @cpu_file_contents;
  96.     if ( open( my $fh, '<:encoding(UTF-8)', $cpu_file ) ) {
  97.         @cpu_file_contents = <$fh>;
  98.         close $fh;
  99.         foreach my $row (@cpu_file_contents) {
  100.             chomp $row;
  101.             if ($row) {
  102.                 my ( $key, $value ) = split /\s*:\s*/msx, $row;
  103.                 if (   ( $key eq 'cpu MHz' )
  104.                     or ( $key eq 'model name' )
  105.                     or ( $key eq 'cpu cores' ) )
  106.                 {
  107.                     $hash{$key} = $value;
  108.                 }
  109.                 elsif ( $key eq 'flags' ) {
  110.                     my @cpu_flags = split / /, $value;
  111.                     $hash{$key} = \@cpu_flags;
  112.                 }
  113.                 else {next}
  114.             }    #end if
  115.         }    #end while
  116.     }    #end if
  117.     else { warn "Could not open file ' $cpu_file' $!"; }
  118.     return %hash;
  119. }    # end sub
  120.  
  121. ###
  122. ### fetching a few lines from /proc/meminfo
  123. ###
  124. sub get_mem_info {
  125.  
  126.     # pulling relevent info from /proc/meminfo
  127.     my %hash;
  128.     my $mem_file = '/proc/meminfo';
  129.     my @mem_file_contents;
  130.     if ( open( my $fh, '<:encoding(UTF-8)', $mem_file ) ) {
  131.         @mem_file_contents = <$fh>;
  132.         close $fh;
  133.         foreach my $row (@mem_file_contents) {
  134.             chomp $row;
  135.             if ($row) {
  136.                 my ( $key, $value ) = split /\s*:\s*/msx, $row;
  137.                 if (   ( $key eq 'MemTotal' )
  138.                     or ( $key eq 'MemFree' )
  139.                     or ( $key eq 'MemAvailable' )
  140.                     or ( $key eq 'SwapTotal' )
  141.                     or ( $key eq 'SwapFree' ) )
  142.                 {
  143.  
  144.                     $hash{$key} = $value;
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     else { warn "Could not open file ' $mem_file' $!"; }
  150.     return %hash;
  151. }    # end sub
  152.  
  153. ###
  154. ### fetching kernel information from /proc/sys/kernel
  155. ###
  156. sub get_kernel_info {
  157.  
  158.     my $directory = '/proc/sys/kernel';
  159.     my %hash;
  160.     my @dir_contents;
  161.  
  162.     # pulling relevant info from /proc/sys/kernel
  163.     opendir( DIR, $directory ) or die $!;
  164.     @dir_contents = readdir(DIR);
  165.     closedir(DIR);
  166.     foreach my $file (@dir_contents) {
  167.         next unless ( -f "$directory/$file" );    #only want files
  168.         if (   ( $file eq 'ostype' )
  169.             or ( $file eq 'osrelease' )
  170.             or ( $file eq 'version' ) )
  171.         {
  172.             if ( open( my $fh, '<:encoding(UTF-8)', "$directory/$file" ) ) {
  173.                 my $row = <$fh>;
  174.                 close $fh;
  175.                 chomp $row;
  176.                 $hash{$file} = $row;
  177.             }
  178.             else { warn "could not open file '$file' $!"; }
  179.         }
  180.     }
  181.     return %hash;
  182. }    #end sub
  183.  
  184. ###
  185. ### fetching files in /boot that start with "kernel" or "vmlinuz"
  186. ###
  187. sub get_boot_dir_info {
  188.     my %hash;
  189.     my $boot_dir = "/boot";
  190.     my @kernel_list;
  191.  
  192.     # pulling list of kernels in /boot
  193.     opendir( DIR, $boot_dir ) or die "cannot access $boot_dir ", $!;
  194.     foreach my $file ( readdir(DIR) ) {
  195.         next unless ( -f "$boot_dir/$file" );    #only want files
  196.         chomp $file;
  197.         if ( $file =~ m/^kernel|^vmlinuz/msx ) {
  198.             push @kernel_list, $file;
  199.         }
  200.     }
  201.     $hash{'available kernels'} = \@kernel_list;
  202.     closedir(DIR);
  203.     return %hash;
  204. }    #end sub
  205.  
  206. ###
  207. ### fetching contents of /var/lib/portage/world
  208. ###
  209. sub get_world_info {
  210.  
  211.     # reading in world file
  212.     my @world_array;
  213.     my %hash;
  214.     my $world_file = '/var/lib/portage/world';
  215.     if ( open( my $fh, '<:encoding(UTF-8)', $world_file ) ) {
  216.         while ( my $row = <$fh> ) {
  217.             chomp $row;
  218.             if ($row) {
  219.                 push( @world_array, $row );
  220.             }
  221.         }
  222.         close $fh;
  223.     }
  224.     else { warn "Could not open file $world_file $!"; }
  225.  
  226.     $hash{'world file'} = \@world_array;
  227.     return \@world_array;
  228. }    #end sub
  229.  
  230. ###
  231. ### fetching versions of key softwares
  232. ###
  233. sub get_version_info {
  234.  
  235.     my %hash;
  236.     my %ebuild_dirs = (
  237.         'portage' => '/var/db/pkg/sys-apps',
  238.         'ego'     => '/var/db/pkg/app-admin',
  239.         'python'  => '/var/db/pkg/dev-lang',
  240.         'gcc'     => '/var/db/pkg/sys-devel',
  241.         'glibc'   => '/var/db/pkg/sys-libs'
  242.     );
  243.  
  244.     ## retrieving portage version
  245.     opendir( DIR, ( $ebuild_dirs{'portage'} ) )
  246.         or die "could not open $ebuild_dirs{'portage'} ", $!;
  247.     my @portage_dir = readdir(DIR);
  248.     closedir(DIR);
  249.     foreach my $folder (@portage_dir) {
  250.         chomp $folder;
  251.         if ( $folder =~ /^portage/msx ) {
  252.             $folder =~ /^portage-(.*)/msx;
  253.             $hash{'portage version'} = $1;
  254.         }
  255.     }
  256.  
  257.     ## retrieving ego version
  258.     opendir( DIR, ( $ebuild_dirs{'ego'} ) )
  259.         or die "could not open $ebuild_dirs{'ego'} ", $!;
  260.     my @ego_dir = readdir(DIR);
  261.     closedir(DIR);
  262.     foreach my $folder (@ego_dir) {
  263.         chomp $folder;
  264.         if ( $folder =~ /^ego/msx ) {
  265.             $folder =~ /^ego-(.*)/msx;
  266.             $hash{'ego version'} = $1;
  267.         }
  268.     }
  269.  
  270.     # retrieving python versions
  271.     my @python_versions;
  272.     opendir( DIR, ( $ebuild_dirs{'python'} ) )
  273.         or die "could not open $ebuild_dirs{'python'} ", $!;
  274.     my @python_dir = readdir(DIR);
  275.     closedir(DIR);
  276.     foreach my $folder (@python_dir) {
  277.         chomp $folder;
  278.         if ( $folder =~ /^python.[^exec]/msx ) {
  279.             $folder =~ /^python-(.*)/msx;
  280.             push @python_versions, $1;
  281.             $hash{'python versions'} = \@python_versions;
  282.         }
  283.     }
  284.  
  285.     # retrieving gcc versions
  286.     my @gcc_versions;
  287.     opendir( DIR, ( $ebuild_dirs{'gcc'} ) )
  288.         or die "could not open $ebuild_dirs{'gcc'} ", $!;
  289.     my @gcc_dir = readdir(DIR);
  290.     closedir(DIR);
  291.     foreach my $folder (@gcc_dir) {
  292.         chomp $folder;
  293.         if ( $folder =~ /^gcc.[^config]/msx ) {
  294.             $folder =~ /^gcc-(.*)/msx;
  295.             push @gcc_versions, $1;
  296.             $hash{'gcc versions'} = \@gcc_versions;
  297.         }
  298.     }
  299.  
  300.     # retrieving glibc versions
  301.     my @glibc_versions;
  302.     opendir( DIR, ( $ebuild_dirs{'gcc'} ) )
  303.         or die "could not open $ebuild_dirs{'gcc'} ", $!;
  304.     my @glibc_dir = readdir(DIR);
  305.     closedir(DIR);
  306.     foreach my $folder (@glibc_dir) {
  307.         chomp $folder;
  308.         if ( $folder =~ /^glibc.[^config]/msx ) {
  309.             $folder =~ /^glibc-(.*)/msx;
  310.             push @glibc_versions, $1;
  311.             $hash{'glibc versions'} = \@glibc_versions;
  312.         }
  313.     }
  314.     return %hash;
  315. }
  316. ################# Helper functions####################
  317. sub get_location_line {
  318.     my $loc_line_data;
  319.     my $path = shift;
  320.     open( my $fhh, '<:encoding(UTF-8)', $path ) or die $!;
  321.     my @file_lines = <$fhh>;
  322.     close $fhh;
  323.     foreach my $thisline (@file_lines) {
  324.         chomp $thisline;
  325.         if ( $thisline =~ /^location/msx ) {
  326.             my ( $trash, $loc_data ) = split( /\s*\=\s*/msx, $thisline );
  327.             chomp $loc_data;
  328.             $loc_line_data = $loc_data;
  329.         }
  330.     }
  331.     return $loc_line_data;
  332. }
  333. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement