diff options
author | Evgeny Zinoviev <me@ch1p.io> | 2021-04-15 16:34:56 +0300 |
---|---|---|
committer | Evgeny Zinoviev <me@ch1p.io> | 2021-04-15 16:34:56 +0300 |
commit | dcf2cbdbeec56a8196b2b4d34848c85ab1530583 (patch) | |
tree | 4c5aabaef9382657f7b0ac0da9763efee9eaa525 |
initial
-rw-r--r-- | README | 6 | ||||
-rwxr-xr-x | main.py | 71 |
2 files changed, 77 insertions, 0 deletions
@@ -0,0 +1,6 @@ +# log-checker + +USAGE + +main.py --token "YOUR_TELEGRAM_TOKEN" --chat-id "YOUR_TELEGRAM_CHAT_ID" --log-file "/var/log/your.log" + @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +import requests +import sys +import traceback +import json +import os + +from argparse import ArgumentParser + + +def readstate(file: str) -> dict: + if not os.path.exists(file): + default_state = {'seek': 0} + writestate(file, default_state) + return default_state + + with open(file, 'r') as f: + return json.loads(f.read()) + + +def writestate(file: str, state: dict): + with open(file, 'w') as f: + f.write(json.dumps(state)) + + +def main(): + # parse arguments + parser = ArgumentParser() + parser.add_argument('--log-file', + type=str, + help='A file to read from') + parser.add_argument('--state-file', + default=('%s/.log-checker.state' % os.getenv('HOME'))) + parser.add_argument('--token', + help='Telegram bot token') + parser.add_argument('--chat-id', + type=int, + help='Telegram chat id (with bot)') + + args = parser.parse_args() + + # read file + state = readstate(args.state_file) + with open(args.log_file, 'r') as f: + # jump to the latest readed position + f.seek(state['seek']) + + # read till the end of the file + content = f.read() + + # save new position + state['seek'] = f.tell() + writestate(args.state_file, state) + + # if got something, send it to telegram + if content != '': + print('sending: %s' % content) + r = requests.post('https://api.telegram.org/bot%s/sendMessage' % args.token, data={ + 'chat_id': args.chat_id, + 'text': content + }) + print(r.status_code) + print(r.text) + + +if __name__ == '__main__': + try: + main() + except: + traceback.print_exc() + sys.exit(1)
\ No newline at end of file |