Linux istemcilerin anlık ram ve cpu kullanımını gösteren bir uygulama
Bir küçük post isteği ile
#!/bin/bash
while true; do sleep 1
hostname=$(hostname)
ip=$(hostname -I | awk '{print $1}')
cpu=$(top -b -n1 | grep "Cpu(s)" | awk '{print $2 + $4}')
total_ram=$(free -m | grep Mem | awk '{print $2}')
usage_ram=$(free -m | grep Mem | awk '{print $3}')
curl -d '{"hostname": "'$hostname'","ip":"'$ip'","cpu":"'$cpu'","tram":"'$total_ram'","uram":"'$usage_ram'"}' -H "Content-Type: application/json" http://127.0.0.1:3000/ > /dev/null 2>&1 &
done
Bu kısmı redis hallediyor. TTL'i iki saniye olacak şekilde ayarladık. Tabi bunun için bir ara katman gerekiyor...
router.post('/', (req, res) => {
var clients_info = [{"ip":req.body.ip,"cpu":req.body.cpu,"tram":req.body.tram,"uram":req.body.uram}]
// console.log(clients_info)
req.redis.setex(req.body.hostname,2,JSON.stringify(clients_info));
return res.json({success:'Client Online',status: 200})
});
/* redis middleware
*/
app.use(function(req,res,next){
req.redis = redisclient;
next();
})
/*end
*/
<script>
$(document).ready(function() {
$('#hostname').DataTable( {
"ajax": 'http://localhost:3000/all',
method: "GET",
"columns": [
{ "data": "hostname",
"mRender": function ( data, type, full ) {
return '<a href="' + window.location+ data +'">'+data+'</a>';
}
},
]
});
} );
</script>
-
Bir küçük Redis Kurulumu
-
Bir adet bash script
-
Ve bu repo
npm start