#!/usr/bin/perl

use Chart::Graph::Gnuplot qw(gnuplot);
use utf8;
use  Mail::Sender;
use open ":encoding(utf8)";
use POSIX ();
use strict;
use warnings;

my (@pr, @ip, @ip0, @proc, @prtm, @fl, @fl0, @f, $date, $time, $lasttime, $lastdate, $sender, $tmpr);
@pr =`sensors | grep Core`; 	# processor core information
@ip =`sensors | grep Physical`; # id 0 information
@prtm =`sensors`; 		# sensors information
foreach my $fg (0..$#prtm) 
{$prtm[$fg]="<p>".$prtm[$fg]."</p>"}
$tmpr = join ("\n", @prtm); 

#print @prtm,"\n";

foreach my $x (0..$#pr) 
{$pr[$x]=~m/\+(\d+)\.\d/; #select the processor core temperature data from the array
$proc[$x]=$&;

open(FILE, ">>/home/temper/t_data/core$x.txt") or die("Не могу открыть файл: $!"); #create a separate file for each processor core
print FILE POSIX::strftime('%H:%M:%S_%d/%m/20%y', localtime)," "; #record the current time and data on the temperature of the processor cores at the end of the file
print FILE $proc[$x],"\n";
close FILE;
}

foreach my $y (0..$#ip) 
{$ip[$y]=~m/\+(\d+)\.\d/; #select the id0 temperature data from the array
$ip0[$y]=$&;
open(FILE, ">>/home/temper/t_data/ip$y.txt") or die("Не могу открыть файл: $!"); #create file for id0 temperature
print FILE POSIX::strftime('%H:%M:%S_%d/%m/20%y', localtime)," ";#record the current time and data on the temperature of the id0 at the end of the file
print FILE $ip0[$y],"\n";
close FILE;
}

foreach my $z (0..$#proc) 
{open CORE, "/home/temper/t_data/core$z.txt"; 
@fl=<CORE>;
if ($#fl>287)#count the number of lines in the file
{system("sed -i 1d /home/temper/t_data/core$z.txt"); 	#delete the line at the beginning of the file
}
close CORE;
}

foreach my $a (0..$#ip0) 
{open(IP, "/home/temper/t_data/ip$a.txt") or die("Не могу открыть файл: $!");
@fl0=<IP>;
if ($#fl0>287) 						#count the number of lines in the file
{system("sed -i 1d /home/temper/t_data/ip0.txt"); 	#delete the line at the beginning of the file 
}
close IP;
}



foreach my $zz (0..$#proc) 
{
	$proc[$zz]=~s/(\+|\.\d)//g; #extract the numerical value of the processor core temperature
#print $proc[$z],"\n";
if ($proc[$zz]>70) #if the core temperature is more than 70, send a message
{
eval 	{
$sender = new Mail::Sender 	{
smtp => 'mail.itep.ru',
from => 'Semenov@itep.ru',
on_errors => 'die',
				};
$sender->Open({
to => 'semenov@itep.ru',
subject => 'Cores temperatures',
ctype=>"text/html",
encoding=>"7bit"
		});

$sender->SendLineEnc("<html>
<head>
<meta  charset=\"windows-1251\">
</head>
<body>
<div style=\"border:5px solid #315576;width:260px;font-size: 10pt;text-align: center; margin: auto; background-color: #e2eced;\">".$tmpr."</div>
</body>
</html>");
#print "Temperatur=",$tmpr,"\n";
$sender->SendLineEnc("\n<p align=center>Regards, Yuri Semenov</p>");
$sender->Close();
	};
if ($@) {
	die "Failed to send the message: $@\n";}
last;}}
foreach my $zzz (0..$#proc)
{if ($proc[$zzz]>90)
{system('/sbin/poweroff');} #if the core temperature is more than 90, turn off computer
}



foreach my $aa (0..$#ip0) 
{$ip0[$aa]=~s/(\+|\.\d)//g;#extract the numerical value of the id0 temperature
#print $ip0[$a],"\n";
#$ip0[$aa]=75;
if ($ip0[$aa]>70){  #if the id 0 temperature is more than 70, send a message
eval 	{
$sender = new Mail::Sender 	{
smtp => 'mail.itep.ru',
from => 'Semenov@itep.ru',
on_errors => 'die',
				};
$sender->Open({
to => 'semenov@itep.ru',
subject => 'Cores temperatures.',
ctype=>"text/html",
encoding=>"7bit"
		});

$sender->SendLineEnc("<html>
<head>
<meta  charset=\"windows-1251\">
</head>
<body>
<div style=\"border:5px solid #315576;width:260px;font-size: 10pt;text-align: center; margin: auto; background-color: #e2eced;\">".$tmpr."</div>
</body>
</html>");

$sender->SendLineEnc("\n<p align=center>Regards, Yuri Semenov</p>");
$sender->Close();
	};
if ($@) {
	die "Failed to send the message: $@\n";}
last;}}
foreach my $aaa (0..$#proc)
{if ($proc[$aaa]>90)
{system('/sbin/poweroff');} #if the core temperature is more than 90, turn off computer
}

open GP, "/home/temper/t_data/core0.txt";
@f=<GP>;
#print @f;
close GP; 
$f[0]=~m/\d{2}\/\d{2}\/\d{4}/; #get the value of the start date of the time range
$date=$&;
$f[0]=~m/\d{2}\:\d{2}\:\d{2}\_\d{2}\/\d{2}\/\d{4}/; #get the value of the start time of the time range
$time=$&;
#print $time;
$f[$#f]=~m/\d{2}\:\d{2}\:\d{2}\_\d{2}\/\d{2}\/\d{4}/;  #get the value of the end time of the time range
$lasttime=$&;
#print $lasttime;
$f[$#f]=~m/\d{2}\/\d{2}\/\d{4}/; #get the value of the end date of the time range
$lastdate=$&;
#print $lastdate;
gnuplot({'title' => 'Core temperature '.$date.'-'.$lastdate, #set the name of the chart
	'y-axis label'=> '°C', #set the name of the y-axis
	'x-axis label' => 'time', #set the name of the x-axis
	'xrange' => '["'.$time.'":"'.$lasttime.'"]', #set the time range on the x-axis
	'xdata' => 'time', #set data on the x-axis
	'output type' => 'png', #specify the format of the graph
	'output file' => '/home/httpd/htdocs/saturn/attacks/ids/gnuplot1.png', #set the path to the chart
	'timefmt' => '%H:%M:%S_%d/%m/%Y', #set the time axis format
	'format' => ['x', '%H:%M'], #set the x-axis format
	'yrange' => '["0":"110"]', #set the range of y-axis values
	'ytics' => [0,10,20,30,40,50,60,70,80,90,100,110], #specify values for the y-axis
	#'xtics' => [0,1,2,3,4,5,6,7,8,9,10],
	 'extra_opts' => join('set timestamp'),},


	[{'title' => 'Core0', #specify the name, style and path to the data file core0
	'style' => 'lines',
	'type' => 'file'}, '/home/temper/t_data/core0.txt'],

		[{'title' => 'Core1', #specify the name, style and path to the data file core1
	'style' => 'lines',
	'type' => 'file'}, '/home/temper/t_data/core1.txt'],

		[{'title' => 'Core2', #specify the name, style and path to the data file core2
	'style' => 'lines',
	'type' => 'file'}, '/home/temper/t_data/core2.txt'],

		[{'title' => 'Core3',
	'style' => 'lines',
	'type' => 'file'}, '/home/temper/t_data/core3.txt'], #specify the name, style and path to the data file core3
);


gnuplot({'title' => 'Package temperature '.$date.'-'.$lastdate, #set the name of the chart

#	'color' => 'blue',
	'y-axis label'=> '°C', #set the name of the y-axis
	'x-axis label' => 'time', #set the name of the x-axis
	'xrange' => '["'.$time.'":"'.$lasttime.'"]', #set the time range on the x-axis
	'xdata' => 'time', #set data on the x-axis
	'output type' => 'png', #specify the format of the graph
	'output file' => '/home/httpd/htdocs/saturn/attacks/ids/gnuplot2.png', #set the path to the chart
	'timefmt' => '%H:%M:%S_%d/%m/%Y', #set the time axis format
	 'format' => ['x', '%H:%M'], #set the x-axis format
	'yrange' => '["0":"110"]', #set the range of y-axis values
	'ytics' => [0,10,20,30,40,50,60,70,80,90,100,110], #specify values for the y-axis
	#'xtics' => [0,1,2,3,4,5,6,7,8,9,10],
	'extra_opts' => join('set timestamp'),},


	[{'title' => 'ip0', #specify the name, style and path to the data file id0
	'style' => 'lines',
	'type' => 'file'}, '/home/temper/t_data/ip0.txt'],
);

#print @proc,"\n";

#open (SENDMAIL, "|/usr/sbin/sendmail -t") or die "sendmail not ready";