diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..cdd492f --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,44 @@ +# This workflow will build a Python project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python + +name: Python + +on: [push] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: Lint code with flake8 + run: | + pip install flake8 + flake8 + + # TODO: it would be nice to have the project published on Pypi for easy installation via pip: + #- name: Publish package + # uses: pypa/gh-action-pypi-publish@release/v1 + # with: + # password: ${{ secrets.PYPI_API_TOKEN }} + + + - name: Build and push Docker image + uses: mr-smithers-excellent/docker-build-push@v6 + with: + # options related to BUILDing the docker image: + dockerfile: ./Dockerfile + multiPlatform: true + platform: linux/amd64,linux/arm64,linux/arm/v7 + image: psmqtt + # options related to PUSHing the docker image: + registry: ghcr.io + username: eschava + password: ${{ secrets.PAT_TOKEN_FOR_GITHUB }} diff --git a/README.rst b/README.rst index a050017..f310b5c 100644 --- a/README.rst +++ b/README.rst @@ -1,50 +1,9 @@ -=============== -About this fork -=============== - -I created this fork to allow psmqtt to run on my TerraMaster NAS. -Use it like this:: - - docker run -d -v :/opt/psmqtt/conf/psmqtt.conf \ - --privileged --hostname tnas \ - ghcr.io/f18m/psmqtt:1.0.2 - -Example of my psmqtt.conf:: - - mqtt_broker = '192.168.1.8' # default: 'localhost' - mqtt_username = 'psmqtt' - mqtt_password = 'psmqtt' - mqtt_clean_session = False - mqtt_qos = 0 - mqtt_retain = False - mqtt_request_topic = 'request' - - schedule = { - "every 5 sec": [ - "cpu_percent", - "virtual_memory/percent", - "sensors_temperatures/rtk_thermal/*", - 'smart/sdc/temperature', - 'smart/sdd/temperature', - ], - "every 60 minutes": "disk_usage/percent/|", # slash replaced with vertical slash - "every 3 hours": { - "boot_time/{{x|uptime}}": "uptime", - } - } - -To push a new docker version with architecture arm64, use:: - - docker buildx build --platform linux/arm64/v8 --tag ghcr.io/f18m/psmqtt:1.0.2 --build-arg USERNAME=root --push . - -(remember to update the tag version) - ======= Summary ======= **PSMQTT** is a cross-platform utility for reporting system and processes -metrics (CPU, memory, disks, network, smart disk data) to an MQTT broker. +metrics (CPU, memory, disks, network, S.M.A.R.T. disk data) to an MQTT broker. **PSMQTT** is written in Python and is based on: @@ -54,16 +13,48 @@ metrics (CPU, memory, disks, network, smart disk data) to an MQTT broker. * `recurrent ` to describe reporting schedule. * `jinja2 ` to format the data. -============ -Installation -============ -Just install the required Python libraries using `pip `_:: +===================== +Installation with pip +===================== + +Clone this repository and then install the required Python libraries using `pip `_:: pip install -r requirements.txt -After you can run main file using:: +(Note that you should consider installing the required libraries in a Python venv to maintain them isolated from the rest of your Python installation. +See `this page for more info `_.) + +Finally you can run **PSMQTT** using:: + + python psmqtt.py + +You may want to install **PSMQTT** as a service/system-daemon, see `this page for more info `_. + + +===================== +Installing on FreeBSD +===================== - python psmqtt.py +See `FreeBSD doc `_ + +===================== +Installing on Windows +===================== + +See `Windows doc `_ + + +================== +Deploy with Docker +================== + +**PSMQTT** is packaged also a multi-arch Docker image. If your system does not have Python3 installed or +you don't want to install **PSMQTT** Python dependencies in your environment, you can launch +a Docker container:: + + docker run -d -v :/opt/psmqtt/conf/psmqtt.conf \ + --privileged --hostname $(hostname) \ + ghcr.io/eschava/psmqtt:latest =============================================== @@ -367,3 +358,16 @@ Useful tasks **smart/nvme0/** - all the device 'nvme0' SMART data, requries SUDO **smart/nvme0/temperature** - 'nvme0' temperature, requires SUDO + + +====================== +Creating a new release +====================== + +New docker releases will be automatically published by the GitHub CI whenever a new tag is released on the project. +Sometimes however it may be useful to publish a docker release manually. +To push manually a new multi-arch docker version, use:: + + docker buildx build --platform linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8, --tag ghcr.io/eschava/psmqtt:1.0.2 --build-arg USERNAME=root --push . + +(remember to update the tag version) diff --git a/doc/KNONW_PROBLEMS.md b/doc/KNOWN_PROBLEMS.md similarity index 100% rename from doc/KNONW_PROBLEMS.md rename to doc/KNOWN_PROBLEMS.md diff --git a/doc/docker.md b/doc/docker.md deleted file mode 100644 index 3d14d47..0000000 --- a/doc/docker.md +++ /dev/null @@ -1,30 +0,0 @@ -# Using Docker to Run `psmqtt` - -Verify your docker installation by: - -```sh -sudo docker run hello-world -``` - -## Build `psmqtt` image - -``` -psmqtt/ > sudo docker build -t "psmqtt:latest" . -``` -Verify that the image is built: -``` - > sudo docker image ls -REPOSITORY TAG IMAGE ID CREATED SIZE -psmqtt latest de06300844f9 About a minute ago 63.1MB -public.ecr.aws/docker/library/python 3.11-alpine 5f9e8f452a5c 5 days ago 51.1MB -``` - -## Execute the `psmqtt` Image in a Container - -[Run the image](https://docs.docker.com/engine/reference/commandline/run/) we -created in the above step passing to it your configuration file -(`/opt/psmqtt/psmqtt.conf`) - -``` -docker run --rm -v /opt/psmqtt/psmqtt.conf:/opt/psmqtt/conf/psmqtt.conf:ro psmqtt -``` diff --git a/src/handlers.py b/src/handlers.py index 4d5514b..8d2f34c 100644 --- a/src/handlers.py +++ b/src/handlers.py @@ -428,11 +428,11 @@ class ProcessesCommandHandler(CommandHandler): ''' ''' - top_cpu_regexp = re.compile("^top_cpu(\[\d+\])*$") - top_memory_regexp = re.compile("^top_memory(\[\d+\])*$") - top_number_regexp = re.compile("^top_[a-z_]+\[(\d+)\]$") - pid_file_regexp = re.compile("^pid\[(.*)\]$") - name_pattern_regexp = re.compile("^name\[(.*)\]$") + top_cpu_regexp = re.compile(r"^top_cpu(\[\d+\])*$") + top_memory_regexp = re.compile(r"^top_memory(\[\d+\])*$") + top_number_regexp = re.compile(r"^top_[a-z_]+\[(\d+)\]$") + pid_file_regexp = re.compile(r"^pid\[(.*)\]$") + name_pattern_regexp = re.compile(r"^name\[(.*)\]$") def __init__(self) -> None: super().__init__('processes') @@ -537,7 +537,7 @@ def handle(self, params:str) -> Payload: info = self.device.__getstate__() if params == '': - return string_from_dict_optionally(info, True) #params.endswith(';') + return string_from_dict_optionally(info, True) elif params == '*': return string_from_dict_optionally(info, False) elif params == '*;': diff --git a/src/task.py b/src/task.py index 24384e5..3c7013a 100644 --- a/src/task.py +++ b/src/task.py @@ -34,8 +34,8 @@ def __init__(self, self.qos = qos self.retain = retain # initialise MQTT broker connection - self.mqttc = paho.Client(paho.CallbackAPIVersion.VERSION1, - client_id, clean_session=clean_session, userdata=self, + self.mqttc = paho.Client(paho.CallbackAPIVersion.VERSION1, + client_id, clean_session=clean_session, userdata=self, protocol=paho.MQTTv311) # protocol=paho.MQTTv5 # see http://www.steves-internet-guide.com/python-mqtt-client-changes/