diff --git a/endpoints/disk.py b/endpoints/disk.py new file mode 100644 index 0000000..5bc504f --- /dev/null +++ b/endpoints/disk.py @@ -0,0 +1,5 @@ +from flask_restful import Resource, abort + +class Disk(Resource): + def get(self): + abort(501, message="Not currently implemented.") diff --git a/endpoints/home.py b/endpoints/home.py new file mode 100644 index 0000000..d6ce449 --- /dev/null +++ b/endpoints/home.py @@ -0,0 +1,11 @@ +from flask_restful import Resource + +class Home(Resource): + def get(self): + payload = [ + { + "Description": "The Thunix API. Please see https://wiki.thunix.net/wiki/api for more information." + } + ] + + return payload diff --git a/endpoints/ip_info.py b/endpoints/ip_info.py new file mode 100644 index 0000000..f32ee2c --- /dev/null +++ b/endpoints/ip_info.py @@ -0,0 +1,32 @@ +from flask_restful import Resource +import psutil + +class Ip_Info(Resource): + def get_ip_addresses(self, family): + ip_addresses = [] + for interface, snics in psutil.net_if_addrs().items(): + for snic in snics: + if snic.family == family: + ip_addresses.append( + { + # We use caps against convention here to make it easier to append + # into the JSON payload + "Interface": interface, + "Address": snic.address, + "Netmask": snic.netmask + } + ) + return ip_addresses + + def get(self): + ipv4 = self.get_ip_addresses(socket.AF_INET) + + payload = [ + { + "Interfaces": [] + } + ] + for addr in ipv4: + payload[0]["Interfaces"].append(addr) + + return payload diff --git a/endpoints/load.py b/endpoints/load.py new file mode 100644 index 0000000..04b55db --- /dev/null +++ b/endpoints/load.py @@ -0,0 +1,14 @@ +from flask_restful import Resource +import psutil + +class Load(Resource): + def get(self): + loadavg = psutil.getloadavg() + payload = [ + { + "1min": loadavg[0], + "5min": loadavg[1], + "15min": loadavg[2] + } + ] + return payload diff --git a/endpoints/memory.py b/endpoints/memory.py new file mode 100644 index 0000000..e68c9fd --- /dev/null +++ b/endpoints/memory.py @@ -0,0 +1,7 @@ +from flask_restful import Resource, abort + +class Memory(Resource): + def get(self): + vmem_usage = psutil.virtual_memory() + smem_usage = psutil.swap_memory() + abort(501, message="Not currently implemented.") diff --git a/endpoints/teapot.py b/endpoints/teapot.py new file mode 100644 index 0000000..e71b27f --- /dev/null +++ b/endpoints/teapot.py @@ -0,0 +1,12 @@ +from flask_restful import Resource + +class Teapot(Resource): + def get(self): + payload = [ + { + "tea": "available", + "height": "short", + "width": "stout" + } + ] + return payload diff --git a/endpoints/uptime.py b/endpoints/uptime.py new file mode 100644 index 0000000..45c212b --- /dev/null +++ b/endpoints/uptime.py @@ -0,0 +1,19 @@ +from flask_restful import Resource +import datetime + +class Uptime(Resource): + def get(self): + with open("/proc/uptime", "r") as f: + secs = float(f.readline().split()[0]) + delta = datetime.timedelta(seconds=secs) + + payload = [ + { + "days": delta.days, + "hours": delta.seconds // 3600, + "minutes": delta.seconds // 60, + "seconds": delta.seconds + } + ] + + return payload diff --git a/thunix_api.py b/thunix_api.py index f57e16a..aa73527 100644 --- a/thunix_api.py +++ b/thunix_api.py @@ -1,127 +1,27 @@ #!/usr/bin/python3 -# thunix_api.py +from flask import Flask +from flask_restful import Resource, Api, abort + +import datetime import flask +import json +import psutil +import socket +import time -from flask import Flask, request, jsonify - -import psutil, datetime, time, socket, json - +from endpoints import disk, home, ip_info, load, memory, teapot, uptime app = Flask(__name__) +api = Api(app) +api.add_resource(disk.Disk, "/disk") +api.add_resource(home.Home, "/") +api.add_resource(ip_info.Ip_Info, "/ip_info") +api.add_resource(load.Load, "/load") +api.add_resource(memory.Memory, "/mem") +api.add_resource(teapot.Teapot, "/teapot") +api.add_resource(uptime.Uptime, "/uptime") -# No endpoint selected -@app.route("/") -def home(): - print ("Content-Type: application/json\n") - payload = [{"Description":"The Thunix API. Please see https://wiki.thunix.net/wiki/api for more information."}] - return jsonify(payload) +if __name__ == "__main__": app.run() - - -# ip_info -@app.route("/ip_info") -def ip_info(): - print ("Content-Type: application/json\n") - def get_ip_addresses(family): - for interface, snics in psutil.net_if_addrs().items(): - for snic in snics: - if snic.family == family: - yield (interface, snic.address, snic.netmask) - - ipv4s = list(get_ip_addresses(socket.AF_INET)) - - - payload = "{\"Interfaces\":[" - i = 0 - for i in range(len(ipv4s)) : - payload = payload + "{" - payload = payload + '"Interface":' + '"' + ipv4s[i][0] + '",' - payload = payload + '"Address":' + '"' + ipv4s[i][1] + '",' - payload = payload + '"Netmask":' + '"' + ipv4s[i][2] + '"' - payload = payload + "}" - i = i + 1 - if (i < len(ipv4s)) : - payload = payload + "," - else: - payload = payload + "" - payload = payload + "]" - payload = payload + "}" - payload = json.loads(payload) - return jsonify(payload) - app.run() - - -# uptime -@app.route("/uptime") -def uptime(): - print ("Content-Type: application/json\n") - with open('/proc/uptime', 'r') as f: - secs = float(f.readline().split()[0]) - day = secs // (24 * 3600) - secs = secs % (24 * 3600) - hour = secs // 3600 - secs %= 3600 - minutes = secs // 60 - secs %= 60 - seconds = secs - payload = [ - { - "days": day, - "hours": hour, - "minutes": minutes, - "seconds": seconds - } - ] - return jsonify(payload) - - app.run() - -# load avg -@app.route("/load") -def loadaverage(): - print ("Content-Type: application/json\n") - loadavg = psutil.getloadavg() - payload=[{"1min":loadavg[0], "5min":loadavg[1], "10min":loadavg[2]}] - - return jsonify(payload),200 - app.run() - -# memory -@app.route("/mem") -def memory(): - print("Content-Type: application/json\n") - vmem_usage = psutil.virtual_memory() - smem_usage = psutil.swap_memory() - #payload = ["Physical Memory" {\ - return jsonify([{"Unavailable":"501"}]),501 - app.run() - -# disk usage -@app.route("/disk") -def disk(): - print("Content-Type: application/json\n") - - return jsonify([{"Unavailable":"501"}]),501 - app.run() - -# teapot -@app.route("/teapot") -def teapot(): - print ("Content-Type: application/json\n") - teapots = [ - { - "tea": "available", - "height": "short", - "width": "stout" - } - ] - return jsonify(teapots),418 - app.run() - - -# main loop -if __name__ == "__main__": # on running python app.py - app.run() # run the flask app -