I'm running a Minecraft server on a Raspberry Pi, but sometimes I want to check the CPU temperature and usage.
Also, I haven't written an article about the Advent calendar more than a week after it was scheduled to be released, so I decided to create something like a resource monitor that allows me to check the CPU temperature even when I'm on the go.
All source code is available on GitHub.
--Model: RaspberryPi4B (Maybe other models can go ...)
vcgencmd
, free
, cat
, grep
I made something like a resource monitor that allows you to check the CPU usage rate, CPU temperature, memory usage rate, etc. of Raspberry Pi on Google Sheets at any time (image below).
The graph is created manually.
The Raspberry Pi side was Python and used Google Apps Script to populate the spreadsheet.
(abridgement)
You can get the code from here [https://raw.githubusercontent.com/Takahiro55555/PiResourceMonitor/master/src/gas/index.gs).
Please paste the code referring to the image below.
Make the same settings as the image below and press the deploy
button
Press the Confirm permission
button in the image below.
Follow the instructions on the screen after that and press the button.
Make a note of the displayed URL as it will be required for preparation on the Raspberry Pi side.
This completes the preparation on the Google Apps Script side.
$ wget https://raw.githubusercontent.com/Takahiro55555/PiResourceMonitor/master/src/raspberry/monitor.py
$ wget https://raw.githubusercontent.com/Takahiro55555/PiResourceMonitor/master/src/raspberry/settings.py
Now, set the URL you noted earlier to GAS_URL
.
settings.py
#Google Apps Script URL
GAS_URL = "URL that you wrote down when preparing the Google Apps Script side"
HOST_NAME = "raspberrypi_test" #Set a unique name
MAX_LINE = 60 #Maximum number of rows on the spreadsheet side (If this number is exceeded, the oldest rows will be deleted)
SAMPLING_INTERVAL_SEC = 3 #Interval to get the status of each resource
This completes the preparation on the Raspberry Pi side.
Let's try it to make sure it works.
$ python3 monitor.py
If a new sheet is created and data is set as shown in the image below, it is successful.
After confirming the operation, stop the program on the Raspberry Pi side using Ctrl + C
etc.
For the sheet name, use the one set in HOST_NAME
in the configuration file.
Feel free to create graphs etc.
As a point, if you do not stop the program on the Raspberry Pi side once, it will be difficult to create a graph. Also, it may be easier to see the sheet for the graph and the sheet created by Google Apps Script separately.
If you do the following, you can keep the program running even if you close the terminal.
$ nohup python3 monitor.py &
CPU temperature and usage rate are obtained and calculated by executing commands from Raspberry Pi. The result is received by GoogleAppsScript and recorded in a spreadsheet.
I implemented it using Python3.
I used the subprocess
module to get the CPU temperature, usage, etc.
I also used requests
as a library for sending data.
On the GoogleAppsScript side, the sheet name from the received data and the column name from the data key are created and set. Therefore, even if the data to be sent changes slightly, it is unlikely that the code on the Google Apps Script side will need to be modified.
In addition, the part where the number of lines of data exceeds MAX_LINE
in the configuration file is deleted in the oldest order (time stamp is irrelevant).
I was using the vmstat
command to get the CPU usage of the Raspberry Pi. However, I was troubled by the phenomenon that the CPU usage rate hardly fluctuated. After a lot of research, I arrived at a certain article.
When the top command is executed, summary information of CPU and memory usage is displayed as a header, but the CPU usage that is displayed first is the average value after starting the OS, so the longer the windless state, the lower the value. Get out. In addition, since the CPU usage rate displayed by vmstat has the same specifications, it is better not to look at the first display when conducting a performance survey.
Also, when I checked vmstat
with the man
command,
The first report produced gives averages since the last reboot. Additional reports give information on a sampling period of length delay. The process and memory reports are instantaneous in either case.
There was a similar description ...
So I gave up using the vmstat
command and went to this site The CPU usage rate is calculated for reference.
Currently, I am acquiring and sending data every 3 seconds, but the order of the data may not be in the order of the time stamp. If the order is out of order, the graph will be out of order, so I'd like to fix it if I feel like it in the future.
-Getting Raspberry Pi CPU frequency, CPU temperature, CPU usage Python script -Use communicate () when receiving output in Python subprocess -Notes on viewing CPU usage with the top command -How to execute in Python at regular intervals and verification -When you get caught in Google Apps Script security diagnosis -[Preserved version] Google Apps Script complete manual for beginners
Recommended Posts