#!/usr/bin/env python3 import subprocess import datetime import json import sys import os now = datetime.datetime.now() date = now.strftime('%Y%m%d') server_name = 'hyein.cayangqu.com' i = 0 #try: # nama_log = f"auto_replytoaccNretweet.log.{sys.argv[2]}" #except: # nama_log = "auto_replytoaccNretweet.log" def twit_sent(): ls_process = subprocess.Popen(["cat",f"/home/telakses/buzz/worker/{sys.argv[1]}/log/{nama_log}"], stdout=subprocess.PIPE, text=True) grep_process = subprocess.Popen(["grep", "post twit sent"], stdin=ls_process.stdout, stdout=subprocess.PIPE, text=True) wc_process = subprocess.Popen(["wc", "-l"], stdin=grep_process.stdout, stdout=subprocess.PIPE, text=True) output, error = wc_process.communicate() return f"Twit sent: {output}" def twit_failed(): ls_process = subprocess.Popen(["cat",f"/home/telakses/buzz/worker/{sys.argv[1]}/log/{nama_log}"], stdout=subprocess.PIPE, text=True) grep_process = subprocess.Popen(["grep", "twit failed"], stdin=ls_process.stdout, stdout=subprocess.PIPE, text=True) wc_process = subprocess.Popen(["wc", "-l"], stdin=grep_process.stdout, stdout=subprocess.PIPE, text=True) output, error = wc_process.communicate() return f"Twit failed: {output}" def quote_tweet(): ls_process = subprocess.Popen(["cat",f"/home/telakses/buzz/worker/{sys.argv[1]}/log/{nama_log}"], stdout=subprocess.PIPE, text=True) grep_process = subprocess.Popen(["grep", "quote twit sent"], stdin=ls_process.stdout, stdout=subprocess.PIPE, text=True) wc_process = subprocess.Popen(["wc", "-l"], stdin=grep_process.stdout, stdout=subprocess.PIPE, text=True) output, error = wc_process.communicate() return f"Twit quoted: {output}" def reply_tweet(): ls_process = subprocess.Popen(["cat",f"/home/telakses/buzz/worker/{sys.argv[1]}/log/{nama_log}"], stdout=subprocess.PIPE, text=True) grep_process = subprocess.Popen(["grep", "reply twit sent"], stdin=ls_process.stdout, stdout=subprocess.PIPE, text=True) wc_process = subprocess.Popen(["wc", "-l"], stdin=grep_process.stdout, stdout=subprocess.PIPE, text=True) output, error = wc_process.communicate() return f"Twit replied: {output}" def first_log(): with open(f"/home/telakses/buzz/worker/{sys.argv[1]}/log/{nama_log}", 'r') as f: lines = f.readlines() firstline = lines[0] firstsplit = firstline.split(" - ",1) output = firstsplit[0] return f"First log: {output}\n" def last_log(): with open(f"/home/telakses/buzz/worker/{sys.argv[1]}/log/{nama_log}", 'r') as f: lines = f.readlines() lastline = lines[-1] lastsplit = lastline.split(" - ",1) output = lastsplit[0] return f"Last log: {output}\n" def twit_failed2(): with open(f"/home/telakses/twitmonitor/twit_failed.txt", 'w') as f: ls_process = subprocess.Popen(["cat",f"/home/telakses/buzz/{sys.argv[1]}/log/{nama_log}"], stdout=subprocess.PIPE, text=True) grep_process = subprocess.Popen(["grep", "twit failed"], stdin=ls_process.stdout, stdout=subprocess.PIPE, text=True) output, error = grep_process.communicate() #print(output) f.write(output) f = open("/home/telakses/twitmonitor/twit_failed.txt", "r") result = { "error_401":0, "error_403":0, "error_400":0, "error_429":0, "error_503":0, } for x in f: error=x.split() raw_error=error[14] clean_error = raw_error[1:4] if(int(clean_error) == 401): result["error_401"]+=1 elif(int(clean_error) == 403): result["error_403"]+=1 elif(int(clean_error) == 400): result["error_400"]+=1 elif(int(clean_error) == 429): result["error_429"]+=1 else: result["error_503"]+=1 return result try: if os.path.exists("/home/telakses/twitmonitor/twit_monitor.txt"): os.remove("/home/telakses/twitmonitor/twit_monitor.txt") else: pass except: #print("File not exists!") pass for i in range(0, 20): try: nama_log = f"buzzworker{i}.log.{sys.argv[2]}" except: nama_log = f"buzzworker{i}.log" #print(f"{nama_log}") with open(f"/home/telakses/twitmonitor/twit_monitor.txt", 'a') as f: f.write(f"Dari log: {nama_log}\n") f.write(first_log()) f.write(last_log()) f.write(twit_sent()) f.write(quote_tweet()) f.write(twit_failed()) #f.write(reply_tweet()) error = twit_failed2() f.write(f"Error 401: {error['error_401']}\n") f.write(f"Error 403: {error['error_403']}\n") f.write(f"Error 400: {error['error_400']}\n") f.write(f"Error 429: {error['error_429']}\n") f.write(f"Error 503: {error['error_503']}\n") f.write("\n")