Guest User

Untitled

a guest
Sep 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.48 KB | None | 0 0
  1. #! /usr/bin/perl
  2. #
  3.  
  4. if ($< != 0)    { die }
  5. if (! $ARGV[0]) { die }
  6.  
  7. use strict;
  8. use DBI;
  9.  
  10. sub s_write () {
  11.     (my $cpu = `sensors | grep "(crit" | awk {'print \$2'}`) =~ s/\+(\d+)\..+\n$/$1/;
  12.     chop(my $sda = `smartctl -a /dev/sda | grep Temperature_Celsius | awk {'print \$10'}`);
  13.     chop(my $sdb = `smartctl -a /dev/sdb | grep Temperature_Celsius | awk {'print \$10'}`);
  14.     chop(my $sdc = `smartctl -a /dev/sdc | grep Temperature_Celsius | awk {'print \$10'}`);
  15.     (my $mhz = (grep { /MHz/ } `lscpu`)[0]) =~ s/.+\s(\d+)\..+\n$/$1/;
  16.     chop(my $psa = `ps aux | wc -l | awk {'print \$1'}`);
  17.     chop(my $mem = `free -m | grep - | awk {'print \$3'}`);
  18.     chop(my $swp = `sed 1d /proc/swaps | awk '{ mem += \$4 } END { print mem }'`);
  19.  
  20.     my $dbh = DBI->connect(
  21.         "DBI:mysql:host=localhost;database=system",
  22.         "system",
  23.         "px0341a"
  24.     ) or die;
  25.  
  26.     $dbh->do("insert into sensors values (default, $cpu, $sda, $sdb, $sdc, $mhz, $psa, $mem, $swp)");
  27.  
  28.     $dbh->disconnect;
  29.  
  30. }
  31.  
  32. sub s_read () {
  33.     my $dbh = DBI->connect(
  34.         "DBI:mysql:host=localhost;database=system",
  35.         "system",
  36.         "px0341a"
  37.     ) or die;
  38.  
  39.     my $query = $dbh->prepare("select * from sensors");
  40.     $query->execute();
  41.  
  42.     print "time\t\t\tcpu\tsda\tsdb\tsdc\tmhz\tproc\tmem\tswap\n";
  43.  
  44.     while (my $r = $query->fetchrow_hashref()) {
  45.         print "$r->{time}\t$r->{cpu}\t$r->{sda}\t$r->{sdb}\t$r->{sdc}\t$r->{mhz}\t$r->{proc}\t$r->{mem}\t$r->{swap}\n";
  46.     }
  47.  
  48.     $dbh->disconnect();
  49. }
  50.  
  51. s_read()  if $ARGV[0] eq "read";
  52. s_write() if $ARGV[0] eq "write";
Add Comment
Please, Sign In to add comment