Flaskでラズパイの状態を確認するWebアプリを作ってみた（其の二）

## 投稿（其の一）のPythonコードのブラッシュアップを実施

https://twitter.com/MashCannu/status/1564541576507826177

#### [Flaskでラズパイの状態を確認するWebアプリを作ってみた（其の一)](https://qiita.com/MashCannu/items/2c4da7bbcf479070b67d)

#### 修正点
+ コマンドの実行結果をテンポラリにテキストファイルへ書き込みしていたのを中止

-----
### get_ip_addr()
```webapp.py
#Get IP address via "ip -4 address" command
@webapp.route('/get_ip_addr')
def get_ip_addr():
    get_ip_addr = subprocess.run(['ip', '-4', 'address'],
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
    cmd_result = get_ip_addr.stdout.decode('utf-8')

    ip_addr_result = []

    cmd_result0 = cmd_result.splitlines()
    for read_line in cmd_result0:
        if re.search('^\s*inet', read_line):
            read_line0 = read_line.lstrip(' ').rstrip('\n').split()
            ip_addr_result.append(read_line0)

    ip_addr_result0 = []

    for read_line in ip_addr_result:
        ip_addr_result0.append((read_line[-1], read_line[1]))

    result_dict = dict(ip_addr_result0)

    return render_template('get_ip_addr.html', result=result_dict)
```

#### 処理結果（定義した関数(def)で実行している処理）
```python

{'eth0': '172.22.3.11/21', 'lo': '127.0.0.1/8', 'wlan0': '172.22.3.12/21'}
```

-----
### get_socket()

```webapp.py
#Get Socket info via "ss -tu" command
@webapp.route('/get_socket')
def get_socket():
    get_socket = subprocess.run(['ss', '-tu'],
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
    cmd_result = get_socket.stdout.decode('utf-8')

    socket_list = []

    cmd_result0 = cmd_result.splitlines()
    for read_line in cmd_result0:
        read_line0 = [*read_line.split(), '']
        socket_list.append(read_line0)

    socket_list1 = []

    for list_value in socket_list:
        socket_list1.append(list_value)

        if 'Netid' in list_value:
            value0 = list_value[4] + '-' + list_value[5]
            value1 = list_value[6] + '-' + list_value[7]
            socket_list1[0].insert(4, value0)
            socket_list1[0].insert(5, value1)
            socket_list1[0].remove('Local')
            socket_list1[0].remove('Peer')
            socket_list1[0].remove('Address:Port')
            socket_list1[0].remove('Address:Port')
            del socket_list1[0][-1]

    return render_template('get_socket.html', result=socket_list1)
```
#### 処理結果（定義した関数(def)で実行している処理）
```python
[['Netid',
  'State',
  'Recv-Q',
  'Send-Q',
  'Local-Address:Port',
  'Peer-Address:Port',
  'Process'],
 ['tcp', 'ESTAB', '0', '0', '127.0.0.1:59162', '127.0.0.1:45667', ''],
 ['tcp', 'ESTAB', '0', '0', '127.0.0.1:59164', '127.0.0.1:45667', ''],
 ['tcp', 'ESTAB', '0', '0', '127.0.0.1:45667', '127.0.0.1:59162', ''],
 ['tcp', 'ESTAB', '0', '0', '127.0.0.1:45667', '127.0.0.1:59164', '']]
```

-----
### get_route()

```webapp.py
# Get IP routing table via "route" command
@webapp.route('/get_route')
def get_route():
    get_route_table = subprocess.run(['route'],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
    cmd_result = get_route_table.stdout.decode('utf-8')

    route_result = []

    cmd_result0 = cmd_result.splitlines()
    for read_line in cmd_result0:
        read_read_line0 = read_line.split()
        route_result.append(read_read_line0)

    del route_result[0]

    return render_template('get_route.html', result=route_result)
```

#### 処理結果（定義した関数(def)で実行している処理）
```python
[['Destination',
  'Gateway',
  'Genmask',
  'Flags',
  'Metric',
  'Ref',
  'Use',
  'Iface'],
 ['default', '172.22.0.1', '0.0.0.0', 'UG', '202', '0', '0', 'eth0'],
 ['default', '172.22.0.1', '0.0.0.0', 'UG', '303', '0', '0', 'wlan0'],
 ['172.22.0.0', '0.0.0.0', '255.255.248.0', 'U', '202', '0', '0', 'eth0'],
 ['172.22.0.0', '0.0.0.0', '255.255.248.0', 'U', '303', '0', '0', 'wlan0']]
```

-----
### get_temp()

```webapp.py
#Get SoC temparature via "vcgencmd" command
@webapp.route('/get_temp')
def get_temp():
    get_temp = subprocess.run(['vcgencmd', 'measure_temp'],
                            stdout = subprocess.PIPE,
                            stderr=subprocess.PIPE)
    value0 = get_temp.stdout.decode('utf-8')
    value1 = value0.split('=')
    value2 = value1[1][:-2]
    cmd_result = f'SoC Temparature : { value2 }C \n'
    return render_template('get_temp.html', result=cmd_result)
```
#### 処理結果（定義した関数(def)で実行している処理）
```python
RaspberryPi4 Temparature : 42.8'°C 
```

-----
### get_disk()

```webapp.py
#Get disk info via "lsbk" & "df -h" command
@webapp.route('/get_disk')
def get_disk():

    #Get disk info via "lsblk" command
    get_block_dev = subprocess.run(['lsblk'],
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
    lsblk_result = get_block_dev.stdout.decode('utf-8')

    result_list0 = []

    lsblk_result0 = lsblk_result.splitlines()
    for read_line in lsblk_result0:
        read_line0 = read_line.rstrip('\n').split()
        result_list0.append(read_line0)

    #Get disk info via "df -h" command
    get_disk_usage = subprocess.run(['df', '-h'],
                                    stdout=subprocess.PIPE,
                                    stderr=subprocess.PIPE)
    df_result = get_disk_usage.stdout.decode('utf-8')

    result_list1 = []

    df_result0 = df_result.splitlines()
    for read_line in df_result0:
        read_line0 = read_line.rstrip('\n').split()
        result_list1.append(read_line0)

    result_list2 = []

    for read_line in result_list1:
        result_list2.append(read_line)

        if 'Filesystem' in read_line:
            h_list = read_line[5] + '-' + read_line[6]
            result_list2[0].insert(5, h_list)
            result_list2[0].remove('Mounted')
            result_list2[0].remove('on')

    return render_template('get_disk.html', lsblk_result=result_list0, 
                            df_result=result_list2)
```

#### 処理結果（定義した関数(def)で実行している処理）
```python
([['NAME', 'MAJ:MIN', 'RM', 'SIZE', 'RO', 'TYPE', 'MOUNTPOINT'],
  ['mmcblk0', '179:0', '0', '29.8G', '0', 'disk'],
  ['├─mmcblk0p1', '179:1', '0', '256M', '0', 'part', '/boot'],
  ['└─mmcblk0p2', '179:2', '0', '29.6G', '0', 'part', '/']],
 [['Filesystem', 'Size', 'Used', 'Avail', 'Use%', 'Mounted-on'],
  ['/dev/root', '30G', '2.6G', '26G', '10%', '/'],
  ['devtmpfs', '3.7G', '0', '3.7G', '0%', '/dev'],
  ['tmpfs', '3.9G', '0', '3.9G', '0%', '/dev/shm'],
  ['tmpfs', '1.6G', '972K', '1.6G', '1%', '/run'],
  ['tmpfs', '5.0M', '4.0K', '5.0M', '1%', '/run/lock'],
  ['/dev/mmcblk0p1', '253M', '31M', '222M', '13%', '/boot'],
  ['tmpfs', '782M', '0', '782M', '0%', '/run/user/1000']])
```