File Coverage

blib/lib/RPi/SysInfo.pm
Criterion Covered Total %
statement 12 54 22.2
branch 0 14 0.0
condition 0 11 0.0
subroutine 4 15 26.6
pod 9 9 100.0
total 25 103 24.2


line stmt bran cond sub pod time code
1             package RPi::SysInfo;
2              
3 9     9   72408 use strict;
  9         66  
  9         255  
4 9     9   43 use warnings;
  9         16  
  9         242  
5              
6 9     9   41 use Carp qw(croak);
  9         17  
  9         1097  
7              
8             our $VERSION = '1.00';
9              
10             require XSLoader;
11             XSLoader::load('RPi::SysInfo', $VERSION);
12              
13 9     9   58 use Exporter qw(import);
  9         23  
  9         7144  
14              
15             our @EXPORT_OK = qw(
16             core_temp
17             cpu_percent
18             mem_percent
19             gpio_info
20             raspi_config
21             network_info
22             file_system
23             pi_details
24             );
25              
26             our %EXPORT_TAGS;
27             $EXPORT_TAGS{all} = [@EXPORT_OK];
28              
29             sub new {
30 0     0 1   return bless {}, shift;
31             }
32             sub core_temp {
33 0 0 0 0 1   shift if $_[0] && $_[0] =~ /RPi::/;
34              
35 0           my ($degree) = @_;
36              
37 0   0       $degree //= 'c';
38              
39             local $SIG{__WARN__} = sub {
40 0     0     my $warning = shift;
41 0 0         if ($warning !~ /Can't exec "vcgencmd"/){
42 0           warn $warning;
43             }
44 0           };
45              
46 0           my $temp = `vcgencmd measure_temp`;
47              
48 0 0         if (! defined $temp){
49 0           croak "issue executing the core temp command, can't continue...\n";
50             }
51              
52 0           $temp =~ s/(temp=)//;
53 0           $temp =~ s/'.*//;
54              
55 0 0 0       if ($degree eq 'f' || $degree eq 'F'){
56 0           $temp = ($temp * 1.8) + 32;
57             }
58              
59 0           chomp $temp;
60 0           return $temp;
61             }
62             sub cpu_percent {
63 0     0 1   return _format(cpuPercent());
64             }
65             sub gpio_info {
66 0 0 0 0 1   shift if $_[0] && $_[0] =~ /RPi::/;
67            
68 0           my ($pins) = @_;
69              
70 0 0         $pins = ! defined $pins
71             ? ''
72             : join ",", @$pins;
73              
74 0           my $info = `raspi-gpio get $pins`;
75 0           chomp $info;
76 0           return $info;
77             }
78             sub mem_percent {
79 0     0 1   return _format(memPercent());
80             }
81             sub network_info {
82 0     0 1   my $netinfo = `ifconfig`;
83 0           chomp $netinfo;
84 0           return $netinfo;
85             }
86             sub raspi_config {
87 0     0 1   my $config = `vcgencmd get_config int`;
88 0           $config .= `vcgencmd get_config str`;
89 0           my $cmd = 'cat /boot/config.txt | egrep -v "^\s*(#|^$)"';
90 0           $config .= `$cmd`;
91              
92 0           chomp $config;
93 0           return $config;
94             }
95             sub file_system {
96 0     0 1   my $fs_info = `df` . "\n";
97 0           $fs_info .= `cat /proc/swaps`;
98 0           return $fs_info;
99             }
100             sub pi_details {
101              
102 0     0 1   my $details;
103              
104 0           $details = "\n"
105             . `cat /sys/firmware/devicetree/base/model`
106             . "\n\n"
107             . `cat /etc/os-release | head -4`
108             . "\n"
109             . `uname -a`
110             . "\n"
111             . `cat /proc/cpuinfo | tail -3`
112             . "Throttled flag : " . `vcgencmd get_throttled`
113             . "Camera : " . `vcgencmd get_camera`;
114              
115 0           return $details;
116             }
117             sub _format {
118 0 0   0     croak "_format() requires a float/double sent in\n" if ! defined $_[0];
119 0           return sprintf("%.2f", $_[0]);
120             }
121             1;
122             __END__