Skip to content

Instantly share code, notes, and snippets.

@14roiron
Last active October 23, 2022 11:44
Show Gist options
  • Save 14roiron/5dc94174aea310d67c7d181e39f87ad8 to your computer and use it in GitHub Desktop.
Save 14roiron/5dc94174aea310d67c7d181e39f87ad8 to your computer and use it in GitHub Desktop.
monitor pikvm screen and save pictures

Monitor screen change on PIKVM to spot error

It would be nice to have it start recording as soon as the static screen changs as it starts to dump information to the console/screen. (idea from: pikvm/pikvm#229)

Install monitor.py in /root

the script need to be updated with correct IP/password and number of files to keep

install cronie for periodic verification

pacman -S cronie

add to crontab -e

*/2 * * * * kvmd-pstrun -- ./monitor.py >/dev/null 2>&1

run

systemctl enable cronie.service

systemctl start cronie.service

How to see files in home assistant

install nfs on pikvm

rw
pacman -Sy --noconfirm nfs-utils

edit the file /etc/exports to add

/var/lib/kvmd/pst/data  10.142.30.100/32(ro,sync)

Need to start nfs server

systemctl enable nfs-server.service systemctl start nfs-server.service

in Home assistant

mkdir /media/pikvm and now to try (in Docker if required): mount -t nfs4 IP:/var/lib/kvmd/pst/data /media/pikvm

Create a command:

shell_command:
  map_pikvm_folder: mount -t nfs4 IP:/var/lib/kvmd/pst/data /media/pikvm

and to finish, you can add an automation to load on home assistant boot:

- id: '1611345337440487'
  alias: 'Mount NFS Folders'
  description: ''
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - service: shell_command.map_pikvm_folder
    data: {}
  mode: single

nfs is comming from: https://community.home-assistant.io/t/adding-media-folder-mapping-from-nas-via-nfs/270763

#!/usr/bin/env python3
# to install
# pacman -S cronie
# add to crontab -e
#*/2 * * * * kvmd-pstrun -- ./monitor.py >/dev/null 2>&1
# run
# systemctl enable cronie.service
# systemctl start cronie.service
import requests # request img from web
import shutil,os # save img locally
import json
import glob
import datetime
import urllib3
urllib3.disable_warnings()
num_max_files = 10
min_diff_size = 30
headers = {'X-KVMD-User': 'admin', 'X-KVMD-Passwd': 'admin'}
url = "https://IP/api/streamer/snapshot"
path = os.environ.get('KVMD_PST_DATA') + '/'
def retrieve_file(file_name:str) -> None:
res = requests.get(url,headers=headers, stream = True,verify=False)
if res.status_code == 200:
with open(file_name,'wb') as f:
shutil.copyfileobj(res.raw, f)
#print('Image sucessfully Downloaded: ',file_name)
else:
pass
print('Image Couldn\'t be retrieved')
dt=datetime.datetime.now()
file_name = f'screen{dt:%y%m%d%H%M-%S}.jpg'
retrieve_file(path+file_name)
files= sorted(glob.glob(path+"*.jpg"))
files_size = [os.stat(file_name).st_size for file_name in files]
if len(files) > 2:
if abs(files_size[-1] - files_size[-2]) < min_diff_size:
os.remove(files[-1])
if len(files)>num_max_files:
os.remove(files[0])
@14roiron
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment