From 5da810245daee9baaba3da028f9a4950f1cb8630 Mon Sep 17 00:00:00 2001 From: Guy Van Sanden Date: Sun, 26 Sep 2021 21:57:09 +0200 Subject: [PATCH] Initial version --- Gruntfile.js | 60 +++++++++++++++++++++++++++++++ control | 6 ++++ package.json | 14 ++++++++ src/config.json.dist | 4 +++ src/tellme.py | 84 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+) create mode 100644 Gruntfile.js create mode 100644 control create mode 100644 package.json create mode 100644 src/config.json.dist create mode 100755 src/tellme.py diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..90c2567 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,60 @@ +module.exports = function(grunt) { + + // Project configuration. + grunt.initConfig({ + pkg: grunt.file.readJSON( 'package.json' ), + + // Tasks here + version: { + control: { + options: { + prefix: 'Version: ' + }, + src: [ 'control' ], + }, + python: { + options: { + prefix: '__version__\\s*=\\s*\"' + }, + src: [ 'src/*.py' ], + } + }, + gitcommit: { + version: { + options: { + message: 'Updated version: <%= pkg.version %>' + }, + files: { + // Specify the files you want to commit + src: ['style.css', 'package.json', 'functions.php'] + } + } + }, + gittag: { + version: { + options: { + tag: 'alertme-<%= pkg.version %>', + message: 'Tagging version <%= pkg.version %>' + } + } + }, + gitpush: { + version: {}, + tag: { + options: { + tags: true + } + } + }, + }); + + // Load all grunt plugins here + grunt.loadNpmTasks('grunt-version'); + + // Bump version task + grunt.registerTask( 'bump', [ 'version' ]); + + // Release task + grunt.registerTask( 'release', [ 'version', 'gitcommit', 'gittag', 'gitpush' ]); + +}; diff --git a/control b/control new file mode 100644 index 0000000..14e8889 --- /dev/null +++ b/control @@ -0,0 +1,6 @@ +Package: telme +Version: 1.0.0 +Maintainer: Guy Van Sanden +Architecture: all +Depends: python +Description: TellMe CLI diff --git a/package.json b/package.json new file mode 100644 index 0000000..a50634d --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "TellMe", + "version": "1.0.0", + "description": "TellMe CLI", + "scripts": { + "dev": "webpack-dev-server --inline --hot" + }, + "author": "Guy Van Sanden ", + "license": "AGPL", + "dependencies": { + "grunt": "~0.4.5", + "grunt-version": "~1.1.0" + } +} diff --git a/src/config.json.dist b/src/config.json.dist new file mode 100644 index 0000000..5ac0f47 --- /dev/null +++ b/src/config.json.dist @@ -0,0 +1,4 @@ +{ + "url": "", + "webhook": "", +} diff --git a/src/tellme.py b/src/tellme.py new file mode 100755 index 0000000..09d4457 --- /dev/null +++ b/src/tellme.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +import time +import sys +import platform +import os +# import urllib.request +import urllib3 +import requests +import http.client as http_client +import argparse +import json +import signal +import subprocess +from xml.etree import ElementTree as etree +from time import gmtime, strftime + +def signal_handler(signal, frame): + print('Exit...') + sys.exit(0) + +def sendmessage (message): + mymessage=hostname.upper() + ': ' +message + + urllib3.disable_warnings() + # http_client.HTTPConnection.debuglevel = 1 + + url = ("%s/api/webhook/%s" % (baseurl, webhook)) + myreq = {} + myreq['message'] = mymessage + r = requests.post(url, json = { 'message': mymessage }) + if r.status_code == 200: + print("Message has been sent successfully") + else: + print("Message sending FAILED") + + +__version__ = "1.0" +versionstring='TellMe v' + __version__ + +signal.signal(signal.SIGINT, signal_handler) + +hostname=platform.node() +home=os.getenv("HOME") + +parser = argparse.ArgumentParser(description='TellMe command line client') +parser.add_argument("-m", "--message", default="Your process finished", help="Message to send") +parser.add_argument('-v', '--version', action='version', version=versionstring) +parser.add_argument('-p', '--pid', action="store", dest="pid", type=int, default=0) +parser.add_argument('-n', '--interval', action="store", dest="interval", type=int, default=5, help="Set the interval of commands like watch that use it") +parser.add_argument('-w', '--watch', action="store", dest="watchcommand", help="Run the given command every n or 5 minutes") +parser.add_argument('-c', '--config', default=home +"/.config/tellme/config.json" ,help="Path to config file") +#print parser.parse_args() +args = parser.parse_args() + +configfile = args.config +# Read config +if not os.path.isfile(configfile): + print("No config file found in %s" %(configfile)) + exit(1) + +config = None +with open(configfile) as cfgfile: + config = json.load(cfgfile) + +baseurl = config['url'] +webhook = config['webhook'] + +message = args.message +if args.pid != 0: + path = "/proc/%i" %(args.pid) + print("Monitoring process %i" %(args.pid)) + while os.path.exists(path): + time.sleep(10) + sendmessage(message) +else: + if(args.watchcommand): + print("Watching %s every %i minutes" %(args.watchcommand,int(args.interval))) + while True: + output = subprocess.Popen(args.watchcommand, shell=True, stdout=subprocess.PIPE).stdout.read() + print(output) + sendmessage("|%s|" %(output)) + time.sleep(60*int(args.interval)) + else: + sendmessage(message)