Giter Club home page Giter Club logo

gnut's Introduction

gNUT (graphical Network UPS Tools)

Graphical webservice for centralized NUT's upsd server.

Inspired by Rshipp's webNUT but rewritten in Django.

Authentication is possible with randomly generated Django superuser password and LDAP.

Currently program supports changing UPS driver's parameters, that are essential for a proper client emergency shutdown. No more fiddling with editing the file manually on the production server!

Development

If you want to contribute to this project, uncomment Debug=True inside .env file and edit it with your needs

It is best suited to test this webservice with a UPS running somewhere nearby

If you don't have an opportunity to have one, you can use the dummy-ups driver

The ups.conf file that is inside the repo is currently configured to use such a driver

You can modify and/or add dummy drivers however you like, they can even share the same or multiple sequence files

[test]
driver = dummy-ups
port = test.seq
desc = "Test driver"

Then edit the sequence file test.seq with your desired UPS outputs loop

You can use any UPS variables here that are possible to access using upsc command.

The following sequence will switch ups.status from On-Line to On-Battery each 15 seconds.

battery.charge: 100
ups.load: 15
battery.charge.low: 10
ups.status: OL
TIMER 15
ups.status: OB
TIMER 15

NOTE: for this time being you NEED to provide battery.charge, ups.load and battery.charge.low otherwise the server won't start

  1. Create virtual environment
python3 -m venv venv
  1. Activate virtual environment

On Linux / MacOS

source venv/bin/activate

On Windows

.\\venv\Scripts\activate.bat
  1. Install requirements
pip3 install -r requirements.txt
  1. Run migrate
python3 manage.py migrate
  1. Create superuser
python3 manage.py createdefaultadmin
  1. The credentials are written to the superuser.txt file
sudo docker exec -it gnut sh -c 'cat superuser.txt'
  1. Run server
python3 manage.py runserver 0.0.0.0:8000

or

gunicorn --bind 0.0.0.0:8000 webNUT.wsgi:application

Installation

At this moment this app is only suited for Docker deploy + apt installable systemd based nut-monitor- available in nut-client package.

For Clients config please read this readme.

Docker deploy

  1. Make a directory for your files and download files needed for starting the application
mkdir -p gnut/ups_config gnut/db_data
wget -O gnut/docker-compose.yml https://raw.githubusercontent.com/mateusz-jastrzebski/gNUT/master/docker-compose.yml
wget -O gnut/.env https://raw.githubusercontent.com/mateusz-jastrzebski/gNUT/master/.env

For servers using the obsolete docker-compose package (instead of docker compose plugin) uncomment the version line in docker-compose.yml

  1. Insert your configs

At this moment gNUT does not let you create configs from the web app (just edit existing ones), keep in touch..

Fill in data in .env (for gNUT configuration) and docker-compose.yml when using containers bundle (mjast/nutgalaxy5500 or other NUT version + reverse proxy)

gnut/ups_config/upsd.users

[<ADMIN_NAME>]
password=<ADMIN_PASSWORD>
actions=SET
upsmon master
[<SLAVE_NAME>]
password=<SLAVE_PASSWORD>
upsmon slave

gnut/ups_config/ups.conf

maxretry = 5

[<UPS_NAME>]
driver = snmp-ups
mibs = apcG5500
port = <UPS_IP>
community = public
snmp_version = v1
pollfreq = 15
desc = "UPS for shutting down non critical servers"
ignorelb
override.battery.charge.low = 50
[<UPS_NAME>]
driver = snmp-ups
mibs = apcG5500
port = <UPS_IP>
community = public
snmp_version = v1
pollfreq = 15
desc = "UPS for shutting down critical servers"
ignorelb
override.battery.charge.low = 30
[<UPS_NAME>]
driver = snmp-ups
mibs = apcG5500
port = <UPS_IP>
community = public
snmp_version = v1
pollfreq = 15
desc = "UPS for shutting ONLY this server"
ignorelb
override.battery.charge.low = 15

gnut/ups_config/upsmon.conf

MINSUPPLIES <NUMBER_OF_UPS_NEEDED_FOR_STAYING_UP>
SHUTDOWNCMD "/sbin/shutdown -h +0"
POLLFREQ 5
POLLFREQALERT 5
HOSTSYNC 15
DEADTIME 15
POWERDOWNFLAG /etc/killpower
RBWARNTIME 43200
NOCOMMWARNTIME 300
FINALDELAY 5
MONITOR <UPS_NAME>@localhost 1 <ADMIN_NAME> <ADMIN_PASSWORD> master
<REPEAT_FOR_EACH_UPS_YOU_WANT_THIS_SYSTEM_TO_RELY_ON>
  1. Generate a secret key and paste it into .env file
tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' </dev/urandom | head -c 32;

gnut/.env

SECRET_KEY=<COPIED_SECRET_KEY>
  1. Setup a service for rootless ups-monitor restart (if host needs to be shutdown)

/usr/lib/systemd/system/upsmon-file-restart.service

[Unit]
Description=File Monitor Service

[Service]
Type=oneshot
ExecStart=/bin/systemctl restart nut-monitor
Restart=no
RestartSec=0

[Install]
WantedBy=multi-user.target

/usr/lib/systemd/system/upsmon-file-restart.path

[Unit]
Description=File Monitor Path Unit

[Path]
PathChanged=<ABSOLUTE_PATH_TO_YOUR_do-not-touch_FILE>

[Install]
WantedBy=multi-user.target

Then edit /etc/nut/nut.conf

MODE=netclient

Delete /etc/nut/upsmon.conf and set a symbolic link from your ups_config folder

  1. Run docker-compose up -d

  2. Default superuser credentials:

sudo docker exec -it gnut sh -c 'cat superuser.txt'
  1. For testing you can either type:
upsc -l

or after a few seconds of starting the gNUT app the UPSes should be visible on the page

Systemd deploy (WIP)

This needs a bunch of care as I mainly moved to deploying this software with Docker

For systemd service deploy you also need to specify a couple of ENV variables for it to work

Ensure env variables are present for the app, then:

  1. Install requirements

to do, need to find linux packages to install eg. python3_xyz

  1. Migrate
python3 manage.py migrate
  1. For a deploy you need a systemd service running

Create and edit /etc/systemd/system/gunicorn.service

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
Type=notify
# the specific user that our service will run as
User=<your_user>
Group=<your_group>
# another option for an even more restricted service is
# DynamicUser=yes
# see http://0pointer.net/blog/dynamic-users-with-systemd.html
RuntimeDirectory=gunicorn
WorkingDirectory=<your_absolute_directory>
ExecStart=/usr/bin/gunicorn webNUT.wsgi
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. Create and edit /etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock
# Our service won't need permissions for the socket, since it
# inherits the file descriptor by socket activation
# only the nginx daemon will need access to the socket
SocketUser=www-data
# Optionally restrict the socket permissions even more.
# SocketMode=600

[Install]
WantedBy=sockets.target
  1. Enable gunicorn.socket
systemctl enable --now gunicorn.socket
  1. Now go ahead and configure a chosen http server

Sample Nginx config (here configured for proxy over a port, not a socket)

server {
    listen 80;


    location /static/ {
        alias <your_absolute_directory>/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }


}

Sample Apache2 config (using UNIX socket)

<VirtualHost *:80>
    ServerName localhost

    DocumentRoot /home/student/Desktop/test-gNUT/gNUT-development

    <Proxy *>
        Order deny,allow
        Require all granted
    </Proxy>

    ProxyPass /static/ !
    ProxyPass / unix:/run/gunicorn.sock|http://localhost/

    Alias /static/ <your_absolue_directory>/static/

    <Directory <your_absolute_directory>>
        Order deny,allow
        Require all granted
        Options -Indexes
    </Directory>

</VirtualHost>
  1. After you start your HTTP Server, the website should be all good and running

  2. If you want to restart the Gunicorn use:

systemctl restart gunicorn.socket

gnut's People

Contributors

mateusz-jastrzebski avatar

Stargazers

 avatar

Watchers

 avatar

gnut's Issues

The app crashes when the docker container starts too quick relatively to the NUT containers

Operations to perform:
  Apply all migrations: auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK
[2024-06-18 12:53:07 +0200] [1] [INFO] Starting gunicorn 22.0.0
[2024-06-18 12:53:07 +0200] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2024-06-18 12:53:07 +0200] [1] [INFO] Using worker: sync
[2024-06-18 12:53:07 +0200] [9] [INFO] Booting worker with pid: 9
[2024-06-18 12:53:07 +0200] [9] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
    worker.init_process()
  File "/usr/local/lib/python3.12/site-packages/gunicorn/workers/base.py", line 134, in init_process
    self.load_wsgi()
  File "/usr/local/lib/python3.12/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
    self.wsgi = self.app.wsgi()
                ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
                    ^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
    return self.load_wsgiapp()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
    return util.import_app(self.app_uri)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/gunicorn/util.py", line 371, in import_app
    mod = importlib.import_module(module)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/app/webNUT/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
    django.setup(set_prefix=False)
  File "/usr/local/lib/python3.12/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.12/site-packages/django/apps/registry.py", line 124, in populate
    app_config.ready()
  File "/app/ups/apps.py", line 16, in ready
    self.ups_list = self.webnut.get_ups_list()
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/ups/nut2utils.py", line 31, in get_ups_list
    'battery': ups_vars['battery.charge'],
               ~~~~~~~~^^^^^^^^^^^^^^^^^^
KeyError: 'battery.charge'
[2024-06-18 12:53:07 +0200] [9] [INFO] Worker exiting (pid: 9)
[2024-06-18 12:53:07 +0200] [1] [ERROR] Worker (pid:9) exited with code 3
[2024-06-18 12:53:07 +0200] [1] [ERROR] Shutting down: Master
[2024-06-18 12:53:07 +0200] [1] [ERROR] Reason: Worker failed to boot.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.