{"id":21534833,"url":"https://github.com/netinvent/udev_monitor","last_synced_at":"2025-04-10T01:43:18.608Z","repository":{"id":62842631,"uuid":"562606400","full_name":"netinvent/udev_monitor","owner":"netinvent","description":"A tool to execute actions upon udev events","archived":false,"fork":false,"pushed_at":"2023-05-03T22:05:18.000Z","size":57,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T02:06:04.313Z","etag":null,"topics":["events","monitor","udev","usb"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/netinvent.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-11-06T21:08:58.000Z","updated_at":"2025-02-16T02:16:46.000Z","dependencies_parsed_at":"2022-11-07T19:15:21.974Z","dependency_job_id":null,"html_url":"https://github.com/netinvent/udev_monitor","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netinvent%2Fudev_monitor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netinvent%2Fudev_monitor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netinvent%2Fudev_monitor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/netinvent%2Fudev_monitor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/netinvent","download_url":"https://codeload.github.com/netinvent/udev_monitor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247749977,"owners_count":20989712,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["events","monitor","udev","usb"],"created_at":"2024-11-24T03:12:48.254Z","updated_at":"2025-04-10T01:43:18.580Z","avatar_url":"https://github.com/netinvent.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# udev_monitor\n# Monitor linux udev events and execute actions on detection\n## Designed to execute actions when USB devices are plugged-in / removed\n\n\n[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\n[![Percentage of issues still open](http://isitmaintained.com/badge/open/netinvent/udev_monitor.svg)](http://isitmaintained.com/project/netinvent/udev_monitor \"Percentage of issues still open\")\n[![linux-tests](https://github.com/netinvent/udev_monitor/actions/workflows/pylint-linux.yaml/badge.svg)](https://github.com/netinvent/udev_monitor/actions/workflows/pylint-linux.yaml)\n[![GitHub Release](https://img.shields.io/github/release/netinvent/udev_monitor.svg?label=Latest)](https://github.com/netinvent/udev_monitor/releases/latest)\n\n\nudev_monitor works with Linux udev and monitors it's events.\nUpon a specific event for a given device (in VENDOR_ID:PRODUCT_ID format), it will execute an action.\n\nudev_monitor has been used to:\n - Re-attach USB devices to virtual machines after they're unplugged - plugged in again\n - Run a full USB reset when device is plugged in (fixes some of the USB UPS that identify as Cypress Semiconductor USB to Serial)\n\n# Setup\n```\npip install udev_monitor\n```\n\n# Quickstart example\nThis is a realworld example to detect most USB UPS and execute a script upon plug-in.\n\nRun script `/usr/local/bin/restart_nut_driver.sh` with argument `0665:5161` everytime USB device with vendor id 0665 and product id 5161 is added or removed\n```\nudev_monitor --devices 0665:5161 --udev-events add,remove --filters=usb --action /usr/local/bin/restart_nut_driver.sh\n```\n\n# Full usage\n```\n--devices           List of comma separated devices to monitor. Example:\n                    '0665:5161, 8086:1234'\n                    If no devices are given, all devices are monitored.\n--udev-events      List of udev events which should trigger and action\n                    Valid actions are: 'add', 'remove', 'change', 'online', 'offline'. Defaults to 'add, change, online'\n--filters           List of comma separated udev monitor filters. Filters are applied with OR logic. Example:\n                    'usb,tty'\n--action            Path to script. Script will get detected device as only argument.\n--timeout           Maximum execution time for script\n--config            Optional path to config file\n```\n\n# Optional configuration file layout\n```\n[UDEV_MONITOR]\ndevices = '0665:5161'\nfilters = 'usb'\naction = '/path/to/script.sh'\nudev_events = 'add'\ntimeout = 3600\n```\n\n# Setting monitor up as a service\n\n- copy file `scripts/udev_monitor@.service` to `/etc/systemd/system`\n- Reload daemons\n- Create configuration file in `/etc/udev_monitor` from example config in `scripts/udev_monitor.conf.example`\n- Launch service\n\nExample:\n```\ncp scripts/udev_monitor\\@.service to /etc/systemd/system\nsystemctl daemon-reload\n\nmkdir /etc/udev_monitor\ncat \u003c\u003c EOF \u003e /etc/udev_monitor/udev_monitor1.conf\ndevices = '0665:5161'\nfilters = 'usb'\naction = '/path/to/script.sh'\nudev_events = 'add'\ntimeout = 3600\nEOF\n\nsystemctl enable --now udev_monitor@udev_monitor1.conf\n```\n\nYou can launch multiple udev_monitor instances by creating multiple conf files and loading them with:\n```\nsystemctl enable --now udev_monitor@umy_ups.conf\nsystemctl enable --now udev_monitor@my_modem.conf\nsystemctl enable --now udev_monitor@my_harddrive.conf\n```\n\n## Further examples\n\n### Automatically attach an USB device (4G modem) to a KVM virtual machine with libvirt and udev_monitor\n\nLet's imagine we have a Sierra 4G model that identifies as 1199:9097, and we would like to attach it to VM modem.vm.local\n\nGrab yourself a copy of usb_reset via `pip install usb_reset`\n\nCreate the following script as `/usr/local/bin/attach_modem.sh` and make it executable with `chmod +x /usr/local/bin/attach_modem.sh`\n\n```\n#!/usr/bin/env bash\n\n# /usr/local/bin/usb_reset.py --reset-device --device 1199:9071\n\nvirsh detach-device sms.badmin.local /root/4G_modem.xml\nvirsh attach-device sms.badmin.local /root/4G_modem.xml\n```\n\nCreate the file `/root/4G_modem.xml` containing:\n```\n    \u003chostdev mode='subsystem' type='usb' managed='yes'\u003e\n      \u003csource\u003e\n        \u003cvendor id='0x1199'/\u003e\n        \u003cproduct id='0x9071'/\u003e\n      \u003c/source\u003e\n    \u003c/hostdev\u003e\n```\n\nNow we must execute that script everytime the USB 4G modem is plugged-in, so we get to re-attach it to the VM.\n\nIn order to do so, let's create the following conf file in `/etc/udev_monitor/modem.conf`\n```\n[UDEV_MONITOR]\ndevices = 1199:9071\nfilters = usb\naction = /usr/local/bin/attach_modem.sh\nudev_events = add\ntimeout = 300\n```\n\nNow let's create a systemd service by copying `udev_monitor@.service` from this git repo to `/etc/systemd/system`\n\nOnce this is done, we just can activate the service with `systemctl enable --now udev_monitor@modem.conf`\n\n### Reset a lawless UPS USB\n\nSome of the USB uninterrupted power supplies (smaller devices) have a quite unreliable USB/Serial interface.\nSometimes it's needed to restart the usb port for the device to work properly.\n\nIn that case, we can use udev_monitor to trigger a usb reset on device plug-in.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetinvent%2Fudev_monitor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnetinvent%2Fudev_monitor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnetinvent%2Fudev_monitor/lists"}