def parse_logs(self): """Parse log files and report errors.""" if os.stat(self.err_file).st_size != 0: notify(subject=f'{self.host} backup parse_logs issues!', attachment=self.err_file) else: self.fail_reset()
def set_pointer(self): """Set hard link pointer.""" if os.path.exists(self.current): os.remove(self.current) try: subprocess.run(['ln', '-sr', self.dst_dir, self.current]) except Exception as e: print(e, 'set_pointer func exception') notify(subject=f'{self.host} backup issues with set_pointer!', body=e)
def health_check(raid, num): fail = 'FAIL' + str(num) if not re.search('clean|active|resync', str(stat)): if not os.path.exists(fail): os.mknod(fail) notify(raid + ' is not clean on ' + host + '!') else: if os.path.isfile(fail): os.remove(fail) notify(raid + ' is now OK on ' + host + '.')
def sync_to_kobila(self, *src_dirs, opts=''): """Sync data from clients to backup server.""" # we make src_dirs as a str, instead of a tuple. src_dirs = ' '.join(src_dirs) cmd = f'time --verbose -a -o {self.log_file} rsync -rlptvhz{opts} ' \ f'--stats --progress --exclude-from=exclude --link-dest={self.current} ' \ f'-e "ssh -p {self.port}" {src_dirs} {self.dst_dir}' try: with open(self.log_file, 'w') as l, open(self.err_file, 'w') as e: subprocess.run(cmd, shell=True, stdout=l, stderr=e) self.set_pointer() self.parse_logs() except Exception as e: print(e, 'sync_to_kobila func exception') notify(subject=f'{self.host} backup failed!', body=e)
def failed(self, errs=''): """Check if max fail bkp attempts is reached.""" if not os.path.exists(self.file): os.mknod(self.file) self.fail_reset() num = open(self.file).read() with open(self.file, 'w') as f: if num == self.fail_cnt: f.write('0') notify( subject=f'{self.fail_cnt} backups failed for {self.host}!', body=errs) else: num = int(num) + 1 f.write(str(num))
# Enter script's working directory os.chdir(os.path.dirname(__file__)) host = socket.gethostname() def health_check(raid, num): fail = 'FAIL' + str(num) if not re.search('clean|active|resync', str(stat)): if not os.path.exists(fail): os.mknod(fail) notify(raid + ' is not clean on ' + host + '!') else: if os.path.isfile(fail): os.remove(fail) notify(raid + ' is now OK on ' + host + '.') # check local mdraids for num in range(3): raid = '/dev/md' + str(num) if os.path.exists(raid): stat = subprocess.run('mdadm -D ' + raid + '|grep "State :"', shell=True, capture_output=True, text=True) health_check(raid, num) else: notify(raid + ' is not a valid device on ' + host)
#!/usr/bin/env python3 """Regex based webpage monitoring.""" import os import re import subprocess from common.notify import notify # Enter script's working directory os.chdir(os.path.dirname(__file__)) # define some vars fail = os.path.isfile('FAIL') ok = 'Web is OK' down = 'Web is DOWN' web = 'https://webpage.com:PORT/subpage.html' keyword = 'your-re.search-keyword' # Check if the initial MonetaWeb App page is accessible data = subprocess.run(['curl', web], capture_output=True, text=True) # check if login page is there if re.search(keyword, str(data)): if fail: os.remove('FAIL') notify(ok) else: if not fail: os.mknod('FAIL') notify(down)