[PYTHON] I made a resource monitor for Raspberry Pi with a spreadsheet

Introduction

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.

Raspberry Pi side environment

--Model: RaspberryPi4B (Maybe other models can go ...)

What I made

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. online_resouce_monitor_on_minecraft05.PNG

How to use

The Raspberry Pi side was Python and used Google Apps Script to populate the spreadsheet.

Preparation on the Google Apps Script side

1. Create a spreadsheet

(abridgement)

2. Open the script editor

スクリプトエディタ01.PNG

3. Copy and paste the code.

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. スクリプトエディタ03.PNG

4. Publish

Make the same settings as the image below and press the deploy button スクリプトエディタ05.PNG

5. Account settings, etc. (first time only)

Press the Confirm permission button in the image below. スクリプトエディタ06.PNG Follow the instructions on the screen after that and press the button.

6. Confirm URL

Make a note of the displayed URL as it will be required for preparation on the Raspberry Pi side. スクリプトエディタ09.PNG

This completes the preparation on the Google Apps Script side.

Preparation on the Raspberry Pi side

1. Get the file

$ wget https://raw.githubusercontent.com/Takahiro55555/PiResourceMonitor/master/src/raspberry/monitor.py
$ wget https://raw.githubusercontent.com/Takahiro55555/PiResourceMonitor/master/src/raspberry/settings.py

2. Edit the configuration file

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.

Operation check and graph creation

1. Confirmation on the Raspberry Pi side

Let's try it to make sure it works.

$ python3 monitor.py

2. Check the spreadsheet

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.

スプレッドシート01.PNG

3. Create a graph as appropriate

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.

4. Start the program

If you do the following, you can keep the program running even if you close the terminal.

$ nohup python3 monitor.py &

Implementation

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.

Raspberry Pi side

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.

Google Apps Script side

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).

At the end

Where I was addicted

1. CPU usage

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.

About the deficiency of this system

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.


References

-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

I made a resource monitor for Raspberry Pi with a spreadsheet
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I made a surveillance camera with my first Raspberry PI.
I made a web server with Raspberry Pi to watch anime
[Electronic work] I made a Suica touch sound detector with Raspberry Pi
I tried to create a button for Slack with Raspberry Pi + Tact Switch
I made a fortune with Python.
Creating a temperature / humidity monitor with Raspberry Pi (pigpio version)
I made a daemon with Python
Using a webcam with Raspberry Pi
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage
I made a lot of files for RDP connection with Python
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
I made a dash docset for Holoviews
Build a Tensorflow environment with Raspberry Pi [2020]
I made a character counter with Python
Try fishing for smelt with Raspberry Pi
Improved motion sensor made with Raspberry Pi
Make a wash-drying timer with a Raspberry Pi
I made a Hex map with Python
I made a life game with Numpy
I made a stamp generator with GAN
Operate an oscilloscope with a Raspberry Pi
Create a car meter with raspberry pi
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a WEB application with Django
I made a neuron simulator with Python
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
Media programming with Raspberry Pi (preparation for audio)
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
Procedure for creating a LineBot made with Python
[Python] I made a Youtube Downloader with Tkinter.
I made a simple Bitcoin wallet with pycoin
I made a downloader for word distributed expression
I made a pet camera that is always connected with WebRTC (Nuxt.js + Python + Firebase + SkyWay + Raspberry Pi)
I made a LINE Bot with Serverless Framework!
I made a random number graph with Numpy
Stock investment analysis app made with Raspberry Pi
I made a peeping prevention product for telework.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a QR code image with CuteR
〇✕ I made a game
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
[AWS] I made a reminder BOT with LINE WORKS
I made a household account book bot with LINE Bot
I made a user management tool for Let's Chat
I made a Christmas tree lighting game with Python
I made a vim learning game "PacVim" with Go
I made a cleaning tool for Google Container Registry