Service for system monitoring

Remark

New Member
Joined
Jun 6, 2022
Messages
2
Reaction score
0
Credits
29
Hello to all!

Currently for learning purposes, I'm trying to develop a service/daemon for RAM, CPU usage of the system (I'm running Ubuntu 20.4). I have reviewed many resources on the web but still cant figure out how to get started. How to take RAM or CPU usage from the system at all? Maybe somehow including bash scripts? I’m confused because I’ve never done that, it would be good to learn and maybe someone could get me on the way...

Thanks for any thoughts.
 


dmidecode provides pretty much anything you could want to know about your system. You could grep the data and import it into your service.
 
Ubuntu has a program called System Monitor I believe this is installed by default this is the Resources tab as you can see it shows CUP History, Memory and Swap usage, Network History - if you click on the Processes Tab ti will show eveything currently running and how much resources each one is using
1.png


Another one is called HTOP which you can get through the Synaptic Package Manager
 
It possible to get ram and cpu usage in many different ways. Here is an example script deriving the outputs from the ps command. It may not be optimally written, but it works:
Code:
#!/bin/sh
#memory used / total memory:
echo -n "Used Memory / Total Memory: "
free -h | awk '/^Mem:/ {print $3 " / " $2}'
#
#---
#temperature of CPU:
echo ""
echo -n "CPU temp: "
sensors | awk '/^Package/ {print $4}' 
#---
#get memory intensive processes
#
#a--all, x--BSD output, c--show just cmd name (not full path), h--no header
#-o-- options, cmd--show cmd, %mem--show mem, sort with minus-- show highest mem users at top
echo ""
echo "Memory Intensive Processes %"
ps axch -o cmd,%mem --sort=-%mem | head -n 5
#
#---
#get cpu intensive processes
echo ""
echo "CPU Intensive Processes %"
ps axch -o cmd,%cpu --sort=-%cpu | head -n 5
#
#---

It's output is:
Code:
Used Memory / Total Memory: 2.4Gi / 3.8Gi

CPU temp: +34.0°C

Memory Intensive Processes %
firefox.real                14.8
firefox-esr                 13.1
Web Content                  6.4
Web Content                  6.2
Web Content                  6.1

CPU Intensive Processes %
firefox.real                 9.9
firefox-esr                  3.3
WebExtensions                2.4
pulseaudio                   2.3
Web Content                  1.5

Here is script deriving the outputs from different sources, /proc/meminfo and iostat from the sysstat package. Again it may not be optimally written, but it works:

Code:
#!/bin/bash
#
#  memory used
#
totalmem=` awk '{print NR, $0}' /proc/meminfo | grep MemTotal |awk '{print $3}' -`
memavail=` awk '{print NR, $0}' /proc/meminfo | grep MemAvailable |awk '{print $3}' -`
memused= echo -n "RAM Used $(( ($totalmem - $memavail)/1024   )) MB |"
#
# cpu% usage
#
cpuused= echo -n "CPU `iostat -c | awk '{print NR, $0}' - | grep ^4 |awk '{print 100-$7}' -`% |"
#
# time
#
timenow=  date +'%a %d %b %Y %H:%M'
#
echo -n "$memused $cpuused $timenow" &

The output is:
Code:
RAM Used 2698 MB |CPU 4.86% |Tue 14 Jun 2022 15:20

If you wish to daemonise the scripts, simply put them into scripted loops, e.g. a while loop, to report their results at suitable intervals.

There are numerous other ways of extracting os data such as from vmstat, sar and the /proc files.
 

Staff online


Top