Sending push notifications to my phone

I had an XMPP server which I used to send Nagios notifications to my phone using XMPP messages. I also hoped to use it for chatting with people, but since everyone uses locked in systems like Facebook and Skype that don't support federation I decided to stop using XMPP.

I decided to use the service Pushover.net instead which is has a mobile app for receiving push notifications.

It works great, you register you app on the website and your device from your mobile. After that it's just a simple HTTP POST request away from sending push messages.

I created a small python script which I use in my Nagios system that sends the alert through Pushover. It's in my github script repo.

Send-Pushover (Check the repo for the latest version)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3

import argparse
import requests
import sys

def push_message(message, app, user):
        data = { "token": app, "user": user, "message": message }
        r = requests.post("https://api.pushover.net/1/messages.json", params=data)

def main(argv):
    parser = argparse.ArgumentParser(description="Push message with Pushover.net")
    parser.add_argument('-a', dest='app_id', required=True, help="Application ID")
    parser.add_argument('-u', dest='user_id', required=True, help="User ID")
    parser.add_argument('-m', dest='message', required=False, default='', help="Message to send, can also be read from stdin")
    args = parser.parse_args(argv)

    if (args.message == '') and not sys.stdin.isatty():
        for line in sys.stdin:
            args.message = args.message + line

    push_message(args.message, args.app_id, args.user_id)

if __name__ == "__main__":
    main(sys.argv[1:])

Get used diskspace on all servers

I had the need to get the used disk space on all our servers today. VMware only shows the peak disk usage and don't account for when files are removed and disk usage is lowered, those metrics was not useful.

To solve this I used SaltStack which we use to configure our servers. The following command returns the information about each disk on the server, like size and used space. The output is saved to disk_usage.txt.

salt "*" disk.usage > disk_usage.txt

The output does not give a good overview of the total disk usage on the server since each disk is displayed individually, and the output format is not ready to be imported in a spreadsheet.

So I created this Python script which parses the disk_usage.txt file and returns a list of server name and the total used space.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import re

with open("disk_usage.txt", "r") as f:
    result = {}
    hostname = ""
    used_space = 0
    get_next_line = False
    current_mount = None
    for line in f:
        if get_next_line:
            size = '\n'.join(line.splitlines()).lstrip(' ')
            get_next_line = False
            if re.match("[0-9]+", size):
                if current_mount.startswith('/'):
                    used_space += int(size) / 1024 / 1024
                else:
                    used_space += int(size) / 1024 / 1024 / 1024

        if re.match("^[a-zA-Z0-9].*", line):
            if hostname is not "":
                print("{}\t{}".format(hostname, used_space))
            hostname = '\n'.join(line.splitlines()).rstrip(':')
            used_space = 0
        if re.match("^[  ]{4}[A-Z/]+", line):
            current_mount = line.lstrip(' ')
        if re.match("^[  ]{8}used:$", line):
            get_next_line = True

The script has to take into account that Salt seems to return the used disk space in kilobytes on Linux servers, but in just bytes on Windows servers.

A tool for distributing password over unsecure channels like email

A recurring problem at work it how to easily send passwords to users. Or sending shared keys for VPN tunnel setups with customers.

I remembered that I had seen a tool for this online that used a webpage where you could enter the password and get a link for retrieve the password that will also remove the password to prevent others from accessing it.

And since I recently started learning Flask which is a nice Python framework for creating websites, I decided to create my own tool for this.

So here it is

http://that-password.steneteg.org/

That Password

Here is the source code for it:

https://github.com/rogst/that-password