# CPU usage shell script



## dcol (Oct 3, 2017)

I am trying to build a CPU Usage shell script and need a shell command that will give me the result of the usage percentage of lets say CPU 0. Seems sysctl kern.cp_time may be the way to go, but not sure of the results that generates.
Maybe a better command, not sure. I am not a guru by any stretch.

Ultimate purpose of this script is to alert me via email if the cpu usage hits 100%. Got every part of the script working except for the CPU Usage calculation.
Any help is greatly appreciated.
Thanks
Dan


----------



## SirDice (Oct 3, 2017)

Look at the iostat(8) and vmstat(8) commands.


----------



## dcol (Oct 3, 2017)

Too generalized for a newbie writing a script. These are possible commands but they display too much info. I need to enter the actual usage number into a variable.
For example, I use the following to give me a number in the variable 'temp' that reflects the temperature of CPU 0

```
temp=$( sysctl dev.cpu.0.temperature | sed -e 's|.*: \([0-9.]*\)C|\1|' )
```
I need the same for CPU 0 usage.


----------



## SirDice (Oct 3, 2017)

dcol said:


> temp=$( sysctl dev.cpu.0.temperature | sed -e 's|.*: \([0-9.]*\)C|\1|' )


Use 

```
temp=$(sysctl -n dev.cpu.0.temperature)
```
No need for the complicated sed(1) pipe. See sysctl(8).



dcol said:


> These are possible commands but they display too much info. I need to enter the actual usage number into a variable.


It's fairly easy to filter out only the data you need with some awk(1) or through a clever tr(1)/cut(1) manipulation.


----------



## dcol (Oct 3, 2017)

The code I posted works and there is no need to reinvent the wheel on that, but thanks for that suggestion.
Looking for a free and easy solution to use in my PFsense firewall which uses FreeBSD which is why I want to just add a simple cron script to perform a simple task on a schedule.
The temperature script works perfectly, no need to change that. Now I want to do the same for the CPU usage, which is why I am asking if anyone knows of a command sequence that will allow me to put that info into a variable. Monitoring tools are overkill because I am only looking for hardware monitoring, not network.
Anyway not sure how to use them with PFsense anyway.
If this cannot be done, then let me know and I will move on.


----------



## rigoletto@ (Oct 3, 2017)

You do need `sysctl dev.cpu.0.cx_usage`.

EDIT: or you can look at:  `sysctl dev.cpu`


----------



## dcol (Oct 3, 2017)

sysctl dev.cpu.0.cx_usage is always at 100, even when system is at 5%, so that would not work. Thought of that earlier.
I looked at all the sysctl.cpu commands, none have what I need. I really don't want to add a packaged monitor system. Don't want the extra overhead.

Let me explain why I need a simple CPU Usage script. When I am running inline Suricata, it can sometimes ramp up the CPU depending on network usage. When the CPU usage gets above 90%, the system is unable to keep up. I want to be notified when this occurs so I can remedy the situation. This usually requires a reboot since just disabling Suricata never seems to lower the CPU usage. Right now I use the temp monitor script because I know that when the CPU starts peaking, the temps rise, so I monitor that. But I would rather monitor the usage since I don't want to keep overheating the system.

I just hoping someone had some coding around that would measure the CPU usage and pass it to a variable.


----------



## rigoletto@ (Oct 4, 2017)

You could use sysutils/conky. Something like this:

conky.conf:

```
out_to_x no
own_window no
out_to_console yes
background no
max_text_width 0
update_interval 2.0
short_units yes
if_up_strictness address
use_spacer left
override_utf8_locale no
cpu_avg_samples 2

TEXT = [[
${cpu cpu0}
]]
```

cpu_usage.sh:

```
#!/usr/bin/env sh

cpu_usage() {

   cpu_0=$(conky -qdc /path/to/conky.conf)

   echo -n "$cpu_0"
}

echo $(cpu_usage)
```

It was not tested but should work with eventual necessary minimal fix (I wrote it now). 

You could even include the cpu temp too.


----------



## dcol (Oct 4, 2017)

Don't want to have to install any additional packages that are not included with PFsense.


----------



## rigoletto@ (Oct 4, 2017)

dcol said:


> Don't want to have to install any additional packages that are not included with PFsense.



You should then ask for that in the PfSense channels...


----------



## dcol (Oct 4, 2017)

You are correct, and I have, but no one seems to provide any answers there. Which is why I came here.


----------



## ljboiler (Oct 4, 2017)

How about using ps -o %cpu to look at how "idle" the machine is, and trigger your monitoring point when it falls below a certain threshold:
	
	



```
ps -o %cpu -p `pgrep -S idle`
```
The percentage seems to be based on 100% * # of cpus (which you can get from sysctl hw.ncpu).

EDIT:  Beware that ps shows snapshot values, not averaged over time, so you'll probably want to do that as part of your monitoring.


----------

