首先使用Mysql服务器(Linux上的LAMP)在您的计算机上安装Nagios。 然后从此链接下载Nagios插件。 Nagios Plugin URL
将下载的文件添加到您安装的nagios目录中。 并且代码在Temperature.php文件中。只需根据数据库信息更改数据库名称和表名。 下面给出的代码。
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Temprature</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
${demo.css}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
table-layout: fixed;
margin-left:8%;
}
th, td {
padding: 15px;
text-align:center;
}
</style>
<script type="text/javascript">
setTimeout(function(){
document.getElementById('refresh').innerHTML="Refreshing....";
},35000)
setTimeout(function(){
location = ''
},40000)
</script>
<?PHP
$maxdattemp="Todays";
$link = mysqli_connect('localhost', 'nagios', 'nagios12345', 'Temperature');
if (!$link) {
die('Not connected : ' . mysql_error());
}
if (!$link) {
die ('Can\'t use foo : ' . mysql_error());
}
else {
if(!isset($_REQUEST['date']))
{
$sql = "SELECT * FROM Temp ORDER BY date DESC , time DESC";
}else
{
$dat=$_REQUEST['date'];
$sql="SELECT * FROM Temp where date='".$dat."' ORDER BY date DESC , time DESC";
}
$arr=array();
$tim=array();
$time=array();
if ($result = mysqli_query($link , $sql)) {
for ($row_no = 0; $row_no <= $result->num_rows - 1; $row_no++) {
$result->data_seek($row_no);
$row = $result->fetch_assoc();
//echo " centigrade = " . $row['centigrade'] . "\n";
$arr[] = floatval($row['centigrade']);
$d=strtotime($row['time']);
$hour = date("H", $d);
$min = date("i", $d);
$sec = date("s", $d);
// $tim[]=
$tim[]= "'".$row['date']." , ".$hour.":".$min.":".$sec."'";
//echo "Created date is " . date("h:i:sa", $d);
//array_push($tim, $row['time']);
//echo $tim[$row_no];
}
}
}
?>
<script type="text/javascript">
$(function () {
$('#container').highcharts({
chart: {
type: 'line'
},
title: {
text: 'Graph'
},
subtitle: {
text: 'Temperature'
},
xAxis: {
categories: [<?php echo $temp= implode(",", $tim); ?>],
title: {
text: null
}
},
yAxis: {
min: 0,
title: {
text: 'Centigrade',
align: 'high'
},
labels: {
overflow: 'justify'
}
},
tooltip: {
valueSuffix: ' °C'
},
plotOptions: {
bar: {
dataLabels: {
enabled: true
}
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -40,
y: 20,
floating: true,
borderWidth: 1,
backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
shadow: true
},
credits: {
enabled: false
},
series: [{
name: 'Temperature',
data: [<?php echo $tempa= implode(",", $arr); ?>]]
}]
});
});
</script>
</head>
<body>
<script src="../../js/highcharts.js"></script>
<script src="../../js/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; max-width: 700px; height: 400px;"></div>
<h3 id="refresh" style="color:#090;margin-left:28%;position:absolute"></h3>
<?php
mysql_connect('localhost','nagios','nagios12345');
mysql_select_db('Temperature') or die( "Unable to select database");
?>
<table style="width:100% !important; border:none !important;">
<col width="40%" />
<col width="50%" />
<tr>
<td style="border:none !important;position:fixed;top:10px;right:5px">
<h3 style="color:#600 ">Latest Temperature Reading:</h3> <?php
$query="SELECT * FROM Temp ORDER BY date DESC , time DESC";
$result=mysql_query($query);
$num_rows = mysql_num_rows($result);
if($num_rows>0)
{
$f1=mysql_result($result,0,"centigrade");
$f2=mysql_result($result,0,"farenheit");
$f3=mysql_result($result,0,"date");
$f4=mysql_result($result,0,"time");
}
//echo $f1.' C'.' , '.$f2.' F'.' , '.' Time: '.$f4.' Date: '.$f3;
if(isset($_REQUEST['date']))
{
$dat=$_REQUEST['date'];
$result1 = mysql_query("SELECT MAX(centigrade) AS cent, MAX(farenheit) AS farh FROM Temp WHERE DATE(date) = '".$dat."'");
$num_rows = mysql_num_rows($result1);
if($num_rows>0)
{
$max=mysql_result($result1,0,"cent");
$farh=mysql_result($result1,0,"farh");
}
$maxtimeresult = mysql_query("SELECT time as tim from Temp WHERE centigrade = '".$max."' AND DATE(date) = '".$dat."' ORDER BY time DESC");
$num_rows = mysql_num_rows($maxtimeresult);
if($num_rows>0)
{
$time=mysql_result($maxtimeresult,0,"tim");
}
$maxdattemp= $dat;
}
else
{
$result1 = mysql_query("SELECT MAX(centigrade) AS cent, MAX(farenheit) AS farh FROM Temp WHERE DATE(date) = CURDATE()");
$num_rows = mysql_num_rows($result1);
if($num_rows>0)
{
$max=mysql_result($result1,0,"cent");
$farh=mysql_result($result1,0,"farh");
}
$maxtimeresult = mysql_query("SELECT time as tim from Temp WHERE centigrade = '".$max."' AND DATE(date) = CURDATE() ORDER BY time DESC");
$num_rows = mysql_num_rows($maxtimeresult);
if($num_rows>0)
{
$time=mysql_result($maxtimeresult,0,"tim");
}
} ?>
<h4 style="color:#090">Date: <?php echo $f3 ?></h4>
<h4 style="color:#090">Time: <?php echo $f4 ?></h4>
<h3 style="color:#00F"><?php echo $f1 ?> °C</h3>
<h3 style="color:#00F"><?php echo $f2 ?> °F</h3>
<?PHP
if(mysql_result($result1,0,"cent")){
?>
<h3 style="color:#F00"><?php echo $maxdattemp; ?> Maximum Temperature:</h3>
<h4 style="color:#090">Time: <?PHP echo $time ?></h4>
<h3 style="color:#F00"><?php echo round($max, 2);?> °C </h3>
<h3 style="color:#F00"><?php echo round($farh, 2);?> °F </h3>
<?PHP }else
{?>
<h3 style="color:#F00">Todays Maximum Temprature:</h3>
<h4 style="color:#090">No Entry for Today</h4>
<?PHP }?>
<form action="Temperature.php" method="post" >
<input type="hidden" id="All" name="All">
<input type="submit" value="Show All">
</form><br/>
<form action="Temperature.php" method="post" style="width:30%;right:1%;position:fixed;">
<span style="color:#090">Select Date:</span>
<input type="date" id="date" name="date">
<input type="submit">
</form>
</td>
</tr>
</table>
<br /> <br /> <br />
<table style="width:50%">
<col width="40%" />
<col width="40%" />
<col width="40%" />
<col width="40%" />
<tr>
<th>Centigrade</th>
<th>Farenheit</th>
<th>Date </th>
<th>Time </th>
</tr>
</table>
<?php
if(isset($_REQUEST['date']))
{
$dat=$_REQUEST['date'];
$query="SELECT * FROM Temp where date='".$dat."' ORDER BY date DESC , time DESC";
$result=mysql_query($query);
}else{
$query="SELECT * FROM Temp ORDER BY date DESC , time DESC";
$result=mysql_query($query);
}
$num=mysql_numrows($result);
mysql_close();
if(mysql_numrows($result)>0){
$i=0;while ($i < $num) {
$f1=mysql_result($result,$i,"centigrade");
$f2=mysql_result($result,$i,"farenheit");
$f3=mysql_result($result,$i,"date");
$f4=mysql_result($result,$i,"time");
?>
<table style="width:50%">
<col width="40%" />
<col width="40%" />
<col width="40%" />
<col width="40%" />
<tr>
<td headers="Centigrade">
<font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font>
</td>
<td headers="farenheit">
<font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font>
</td>
<td headers="date">
<font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font>
</td>
<td headers="time">
<font face="Arial, Helvetica, sans-serif"><?php echo $f4; ?></font>
</td>
</tr>
</table>
<?php $i++;
}
}else
{?>
<h4 style="margin-left:27%"><font face="Arial, Helvetica, sans-serif"><?php echo 'No Record Found'; ?></font>
</h4>
<?PHP
}
?>
</body>
</html>
您可以在Nagios中找到侧栏,并在报告下找到Mysql报告选项,图表和温度数据包含最新的温度读数和今天的最高温度。 谢谢!
答案 0 :(得分:0)
我认为您需要告诉实体框架您的类已更改:
h.quantity = t.quantity;
context.DB.Entry(h).State = EntityState.Modified;
context.DB.SaveChanges();