Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/maint/bin
File: userstats.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
# post CloudLinux user stats to grafana
[1] Fix | Delete
from argparse import ArgumentParser
[2] Fix | Delete
import subprocess
[3] Fix | Delete
import json
[4] Fix | Delete
import requests
[5] Fix | Delete
import traceback
[6] Fix | Delete
[7] Fix | Delete
from socket import gethostname
[8] Fix | Delete
from datetime import datetime
[9] Fix | Delete
from pathlib import Path
[10] Fix | Delete
from time import time
[11] Fix | Delete
[12] Fix | Delete
maint_dir = Path("/opt/maint/etc")
[13] Fix | Delete
GLOBAL_IDS = {}
[14] Fix | Delete
[15] Fix | Delete
[16] Fix | Delete
def run_script(command, capture_output=True, check_exit_code=True):
[17] Fix | Delete
proc = subprocess.run(command, capture_output=capture_output)
[18] Fix | Delete
out = proc.stdout
[19] Fix | Delete
if proc.returncode != 0 and check_exit_code:
[20] Fix | Delete
raise Exception(
[21] Fix | Delete
f"Command returned exit code {proc.returncode}" f": '{command}'"
[22] Fix | Delete
)
[23] Fix | Delete
[24] Fix | Delete
return out.decode("utf-8")
[25] Fix | Delete
[26] Fix | Delete
[27] Fix | Delete
class StatsAPI:
[28] Fix | Delete
def __init__(self, conf):
[29] Fix | Delete
self.url = conf["host"]
[30] Fix | Delete
self.key = conf["key"]
[31] Fix | Delete
[32] Fix | Delete
def post_stats(self, stats, period):
[33] Fix | Delete
server = gethostname()
[34] Fix | Delete
data = dict(server=gethostname(), period=period, stats=stats)
[35] Fix | Delete
[36] Fix | Delete
response = requests.post(
[37] Fix | Delete
f"{self.url}/v1/userstats/?server={server.split('.')[0]}",
[38] Fix | Delete
json=data,
[39] Fix | Delete
headers={"Authorization": f"{self.key}"},
[40] Fix | Delete
)
[41] Fix | Delete
return response
[42] Fix | Delete
[43] Fix | Delete
[44] Fix | Delete
def fatal_error(msg):
[45] Fix | Delete
print(f"Error: {msg}")
[46] Fix | Delete
msg = f"{datetime.now()}: {msg}"
[47] Fix | Delete
[48] Fix | Delete
err_file = maint_dir / "userstats.err"
[49] Fix | Delete
footer = (
[50] Fix | Delete
"Remove this file if you've reviewed the error message"
[51] Fix | Delete
"or fixed the issue."
[52] Fix | Delete
)
[53] Fix | Delete
err_file.write_text(f"{msg}\n\n{footer}")
[54] Fix | Delete
exit(1)
[55] Fix | Delete
[56] Fix | Delete
[57] Fix | Delete
def load_conf():
[58] Fix | Delete
conf = maint_dir / "userstats.cfg"
[59] Fix | Delete
if not conf.exists():
[60] Fix | Delete
fatal_error(f"Configuration file {conf} not found.")
[61] Fix | Delete
return json.loads(conf.read_text())
[62] Fix | Delete
[63] Fix | Delete
[64] Fix | Delete
def parse_args():
[65] Fix | Delete
parser = ArgumentParser(description="Export user usage statistics")
[66] Fix | Delete
[67] Fix | Delete
parser.add_argument(
[68] Fix | Delete
"--check",
[69] Fix | Delete
action="store_true",
[70] Fix | Delete
)
[71] Fix | Delete
[72] Fix | Delete
return parser.parse_args()
[73] Fix | Delete
[74] Fix | Delete
[75] Fix | Delete
def check():
[76] Fix | Delete
err_file = maint_dir / "userstats.err"
[77] Fix | Delete
[78] Fix | Delete
status = run_script(["systemctl", "status", "lvestats"])
[79] Fix | Delete
if "Active: active" not in status:
[80] Fix | Delete
print("CRITICAL: lvestats is not running.")
[81] Fix | Delete
exit(2) # CRITICAL
[82] Fix | Delete
[83] Fix | Delete
if err_file.exists():
[84] Fix | Delete
now = datetime.now().astimezone().timestamp()
[85] Fix | Delete
delta = now - err_file.stat().st_mtime
[86] Fix | Delete
if delta < 60 * 15: # 5 minutes
[87] Fix | Delete
print(
[88] Fix | Delete
f"CRITICAL: Userstats failfile tripped. Error in: {err_file} "
[89] Fix | Delete
"When the issue is fixed - the command runs normally - remove the file"
[90] Fix | Delete
)
[91] Fix | Delete
exit(2) # CRITICAL
[92] Fix | Delete
else:
[93] Fix | Delete
print(
[94] Fix | Delete
f"WARNING: {err_file} exists and is {int(delta/60)} minutes old."
[95] Fix | Delete
)
[96] Fix | Delete
[97] Fix | Delete
exit(1) # WARNING
[98] Fix | Delete
[99] Fix | Delete
if "ERROR" in status:
[100] Fix | Delete
print("WARNING: lvestats service encountered an error.")
[101] Fix | Delete
exit(1) # WARNING
[102] Fix | Delete
[103] Fix | Delete
print("OK.")
[104] Fix | Delete
exit(0)
[105] Fix | Delete
[106] Fix | Delete
[107] Fix | Delete
def detect_period(average_period: int, touch_threshold=False):
[108] Fix | Delete
"""
[109] Fix | Delete
Use touch file to detect period
[110] Fix | Delete
[111] Fix | Delete
touch_threshold determines if we should touch the file regardless of
[112] Fix | Delete
average_period. True means we only touch it if delta is > average_period.
[113] Fix | Delete
"""
[114] Fix | Delete
touch_file = Path(f"/run/clstats-touch-{average_period}")
[115] Fix | Delete
if not touch_file.exists():
[116] Fix | Delete
touch_file.touch()
[117] Fix | Delete
return average_period
[118] Fix | Delete
else:
[119] Fix | Delete
mtime = int(touch_file.stat().st_mtime)
[120] Fix | Delete
now = int(time())
[121] Fix | Delete
time_delta = now - mtime
[122] Fix | Delete
minutes = int(round(time_delta / 60))
[123] Fix | Delete
if not touch_threshold or minutes >= average_period:
[124] Fix | Delete
touch_file.touch()
[125] Fix | Delete
return max(average_period, minutes)
[126] Fix | Delete
else:
[127] Fix | Delete
return minutes
[128] Fix | Delete
[129] Fix | Delete
[130] Fix | Delete
def get_stats(period):
[131] Fix | Delete
stats = [
[132] Fix | Delete
"/sbin/cloudlinux-statistics",
[133] Fix | Delete
"--period",
[134] Fix | Delete
period,
[135] Fix | Delete
"--json",
[136] Fix | Delete
]
[137] Fix | Delete
usage_stats = run_script(stats)
[138] Fix | Delete
usage_data = json.loads(usage_stats)
[139] Fix | Delete
rows = []
[140] Fix | Delete
[141] Fix | Delete
userdata = usage_data["users"]
[142] Fix | Delete
[143] Fix | Delete
for user_details in userdata:
[144] Fix | Delete
usage = user_details["usage"]
[145] Fix | Delete
limits = user_details["limits"]
[146] Fix | Delete
faults = user_details["faults"]
[147] Fix | Delete
username = user_details["username"]
[148] Fix | Delete
domain = user_details["domain"]
[149] Fix | Delete
reseller = user_details["reseller"]
[150] Fix | Delete
row = {}
[151] Fix | Delete
[152] Fix | Delete
def convert_int(stat):
[153] Fix | Delete
return min(int(round(stat["lve"])), 2147483647)
[154] Fix | Delete
[155] Fix | Delete
usage_convert_table = {
[156] Fix | Delete
"cpu": lambda x: min(int(round(x["lve"])), 65535),
[157] Fix | Delete
"ep": lambda x: min(int(x["lve"]), 65535),
[158] Fix | Delete
"io": lambda x: min(int(x["lve"] / 1024 / 1024), 65535), # mb
[159] Fix | Delete
"iops": lambda x: min(int(x["lve"]), 65535),
[160] Fix | Delete
"pmem": lambda x: min(int(x["lve"] / 1024 / 1024), 65535), # mb
[161] Fix | Delete
"vmem": lambda x: min(int(x["lve"] / 1024 / 1024), 65535), # mb
[162] Fix | Delete
"nproc": lambda x: min(int(x["lve"]), 65535),
[163] Fix | Delete
}
[164] Fix | Delete
row["usage"] = {}
[165] Fix | Delete
for key, value in usage.items():
[166] Fix | Delete
converted = usage_convert_table[key](value)
[167] Fix | Delete
key_name = key
[168] Fix | Delete
if key in ["io", "pmem", "vmem"]:
[169] Fix | Delete
key_name = f"{key}_m"
[170] Fix | Delete
[171] Fix | Delete
row["usage"][key_name] = converted
[172] Fix | Delete
[173] Fix | Delete
row["limits"] = {}
[174] Fix | Delete
for key, value in limits.items():
[175] Fix | Delete
row["limits"][key] = convert_int(value)
[176] Fix | Delete
[177] Fix | Delete
row["faults"] = {}
[178] Fix | Delete
for key, value in faults.items():
[179] Fix | Delete
row["faults"][key] = convert_int(value)
[180] Fix | Delete
[181] Fix | Delete
row["domain"] = domain
[182] Fix | Delete
row["reseller"] = reseller
[183] Fix | Delete
row["username"] = username
[184] Fix | Delete
rows.append(row)
[185] Fix | Delete
[186] Fix | Delete
return rows
[187] Fix | Delete
[188] Fix | Delete
[189] Fix | Delete
def main():
[190] Fix | Delete
args = parse_args()
[191] Fix | Delete
if args.check:
[192] Fix | Delete
check()
[193] Fix | Delete
[194] Fix | Delete
if not Path("/sbin/cloudlinux-statistics").exists():
[195] Fix | Delete
fatal_error(
[196] Fix | Delete
"cloudLinux-statistics not found. "
[197] Fix | Delete
"This tool only works on CloudLinux systems."
[198] Fix | Delete
)
[199] Fix | Delete
conf = load_conf()
[200] Fix | Delete
try:
[201] Fix | Delete
api = StatsAPI(conf["api"])
[202] Fix | Delete
[203] Fix | Delete
period = detect_period(1)
[204] Fix | Delete
rows = get_stats(f"{period}m")
[205] Fix | Delete
if rows:
[206] Fix | Delete
post1 = api.post_stats(rows, 1)
[207] Fix | Delete
if post1.status_code != 200:
[208] Fix | Delete
fatal_error(
[209] Fix | Delete
f"Failed to post user stats to Grafana: {post1.status_code}\n{post1.text}"
[210] Fix | Delete
)
[211] Fix | Delete
[212] Fix | Delete
except Exception as e:
[213] Fix | Delete
stack_trace = traceback.format_exc()
[214] Fix | Delete
fatal_error(f"An error occurred: {str(e)}\n{stack_trace}")
[215] Fix | Delete
[216] Fix | Delete
[217] Fix | Delete
if __name__ == "__main__":
[218] Fix | Delete
main()
[219] Fix | Delete
[220] Fix | Delete
[221] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function