LoginSignup
1
1

More than 1 year has passed since last update.

Linuxのハードウェア状況コマンドが覚えられずGUIアプリ作った話

Last updated at Posted at 2021-02-01

0.初めに

私は、非エンジニアのドシロウトです。しかも記憶力が弱いです。

3か月前からASUS X205TAにLinux Mint XFCE 64bitを使っていますが、ハードウェア状況を確認するコマンドが、なかなか覚えられません。

具体的には、以下のコマンド達です。

  • CPU使用率 mpstat -P ALL
  • CPU等温度 sensors
  • メモリ使用状況 free -h
  • ディスク使用率 df -h
  • バッテリー情報 upower -i /org/freedesktop/UPower/devices/battery_BATC
  • CPU属性 lscpu

月に1回くらいしか使わないコマンドが多く、全然覚えられず、一々ググって非効率です。

そこでGUIのアプリみたいのをLinux Mintのデスクトップから起動できないか調べ始めました。

1.見つけた記事

PythonでGUIアプリの作成 - Qiita

この記事がともかく参考…というか丸パクリ…汗…で、すごく役立ちました。ありがとうございます。

記事の説明内容の通り、Eelというライブラリを使っています。

記事に従い、以下のディレクトリ構成で作ってます。

/home/xxx/eel/eel_sysinfo/
│  app.py
└─web
    ├─css
    │      index.css
    ├─html
    │      index.html
    ├─image
    │      sysinfo.png
    └─js
           index.js

2.プログラム

パクって作ったので、あまり説明することがありませんが、一応貼っておきます。

  • app.py

    OSコマンドを使う為、subprocessを使っています。

app.py
import eel
import time
from time import ctime
from datetime import datetime
import subprocess
from subprocess import PIPE


# expose to javascript
@eel.expose
def ask_python_from_js_get_sysinfo():

    allres = "<pre>";

    try:
        proc = subprocess.run("date", shell=True, stdout=PIPE, stderr=PIPE, text=True)
        date = proc.stdout
        allres += 'DATE: {}'.format(date)+"\n\n"
        proc = subprocess.run("mpstat -P ALL", shell=True, stdout=PIPE, stderr=PIPE, text=True)
        cpu = proc.stdout
        allres += "\nCPU使用率\n"+cpu+"\n"
        proc = subprocess.run("sensors", shell=True, stdout=PIPE, stderr=PIPE, text=True)
        cputemp = proc.stdout
        allres += "\nCPU等温度\n"+cputemp+"\n"
        proc = subprocess.run("free -h", shell=True, stdout=PIPE, stderr=PIPE, text=True)
        memory = proc.stdout
        allres += "\nメモリ使用状況\n"+memory+"\n"
        proc = subprocess.run("df -h --total", shell=True, stdout=PIPE, stderr=PIPE, text=True)
        disk = proc.stdout
        allres += "\nディスク使用率\n"+disk+"\n"
        proc = subprocess.run("upower -i /org/freedesktop/UPower/devices/battery_BATC", shell=True, stdout=PIPE, stderr=PIPE, text=True)
        battery = proc.stdout
        allres += "\nバッテリー情報\n"+battery+"\n"
        proc = subprocess.run("lscpu", shell=True, stdout=PIPE, stderr=PIPE, text=True)
        cpuinfo = proc.stdout
        allres += "\nCPU属性\n"+cpuinfo+"\n"
    except:
        allres += "/nError./n"
    finally:
        # return
        # call javascript function
        allres += "</pre>"
        eel.run_js_from_python_sysinfo(allres)

# initialize the folder which contents html,js,css,etc
eel.init("web")

# start app
eel.start("html/index.html")
  • index.css
index.css
pre {
    font-size: 22px;
}
  • index.html

ハードウェア状況を30秒ごとに取得するようにしています。

index.html
<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../css/index.css">
    <script type="text/javascript" src="/eel.js"></script>
    <script src="../js/index.js"></script>
    <title>稼働情報一覧</title>
</head>

<body>
    <img src="../image/sysinfo.png">
    <div class="box">

        <div id="sysinfo">

        </div>

    </div>

    <script>
        window.onload = function() {
            getsysinfo();
            setInterval(getsysinfo, 30000);
        };
    </script>
</body>
</html>
  • index.js
index.js
// this function is called when button is clicked
function getsysinfo() {
    console.log('getsysinfo');
    // call python function
    eel.ask_python_from_js_get_sysinfo();
}

// this function will be executed from python
// msg is the argument received from python side
// it will show the message received to user
eel.expose(run_js_from_python_sysinfo);
function run_js_from_python_sysinfo(msg) {
    console.log('from python sysinfo');
    document.getElementById("sysinfo").innerHTML = msg;
}

3.デスクトップアイコン

せっかくGUIアプリを作ったのでターミナルからではなく、デスクトップアイコンから起動したいと思いました。

あまり参考になる記事が無かったので、他のアプリのデスクトップアイコンを調べて確認。

ほかのディストリビューションではどうか知りませんが、Linux Mint 20 Xfceでは以下のようなファイルを作り、デスクトップに置くと起動アイコンとして使えました。

s-info.desktop
[Desktop Entry]
Version=1.0
Name=S-INFO
Comment=System information
Exec=/usr/bin/python3 /home/xxx/eel/eel_sysinfo/app.py
Path=/home/xxx/eel/eel_sysinfo/
Icon=/home/xxx/eel/eel_sysinfo/web/image/sysinfo.png
Terminal=false
Type=Application
Categories=Utility;Application;

4.終わりに

一応動作テストの動画です。

Linux Mint 20 xfce 64でPython3+Eelを使ったGUIアプリのテスト風景(音、説明なし) - YouTube

以 上

1
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1