forked from Sysdeploy/idlers-agent
90 lines
2.8 KiB
Python
90 lines
2.8 KiB
Python
|
import os
|
||
|
import requests
|
||
|
import psutil
|
||
|
import tldextract
|
||
|
import dns.resolver
|
||
|
import logging
|
||
|
|
||
|
class ServerData:
|
||
|
def __init__(self):
|
||
|
self.hostname = os.uname().nodename
|
||
|
self.public_ip = self.get_public_ip()
|
||
|
self.domain = self.get_domain()
|
||
|
self.nameservers = self.get_nameservers()
|
||
|
logging.basicConfig(level=logging.INFO)
|
||
|
|
||
|
def get_ram_and_disk(self):
|
||
|
ram = psutil.virtual_memory().total >> 20
|
||
|
disk = psutil.disk_usage('/').total >> 30
|
||
|
logging.info(f"RAM: {ram}MB, Disk: {disk}GB")
|
||
|
return ram, disk
|
||
|
|
||
|
def get_cpu_count(self):
|
||
|
cpu_count = psutil.cpu_count()
|
||
|
logging.info(f"CPU Count: {cpu_count}")
|
||
|
return cpu_count
|
||
|
|
||
|
def get_bandwidth(self):
|
||
|
bandwidth = 2000
|
||
|
logging.info(f"Bandwidth: {bandwidth}")
|
||
|
return bandwidth
|
||
|
|
||
|
def get_public_ip(self):
|
||
|
try:
|
||
|
response = requests.get('https://api.ipify.org')
|
||
|
response.raise_for_status()
|
||
|
return response.text
|
||
|
except requests.exceptions.RequestException as e:
|
||
|
logging.error(f"Failed to get public IP: {e}")
|
||
|
return '127.0.0.1'
|
||
|
|
||
|
def get_domain(self):
|
||
|
extracted = tldextract.extract(self.hostname)
|
||
|
domain = "{}.{}".format(extracted.domain, extracted.suffix)
|
||
|
logging.info(f"Domain: {domain}")
|
||
|
return domain
|
||
|
|
||
|
def get_nameservers(self):
|
||
|
try:
|
||
|
answers = dns.resolver.resolve(self.domain, 'NS')
|
||
|
return [str(rdata) for rdata in answers]
|
||
|
except Exception as e:
|
||
|
logging.error(f"Failed to get nameservers: {e}")
|
||
|
return []
|
||
|
|
||
|
def get_os(self):
|
||
|
os_id = 27
|
||
|
logging.info(f"OS ID: {os_id}")
|
||
|
return os_id
|
||
|
|
||
|
def create_post_data(self):
|
||
|
ram, disk = self.get_ram_and_disk()
|
||
|
post_data = {
|
||
|
"server_type": 1,
|
||
|
"os_id": self.get_os(),
|
||
|
"provider_id": 10,
|
||
|
"location_id": 15,
|
||
|
"ssh_port": 22,
|
||
|
"ram": ram >> 10, # convert to GB
|
||
|
"ram_as_mb": ram, # in MB
|
||
|
"disk": disk, # in GB
|
||
|
"disk_as_gb": disk, # in GB
|
||
|
"cpu": self.get_cpu_count(),
|
||
|
"bandwidth": self.get_bandwidth(),
|
||
|
"was_promo": 1,
|
||
|
"active": 1,
|
||
|
"show_public": 0,
|
||
|
"owned_since": "2022-01-01",
|
||
|
"ram_type": "GB",
|
||
|
"disk_type": "GB",
|
||
|
"currency": "USD",
|
||
|
"price": 4,
|
||
|
"payment_term": 1,
|
||
|
"hostname": self.hostname,
|
||
|
"next_due_date": "2022-02-01",
|
||
|
"ns1": self.nameservers[0] if self.nameservers else "",
|
||
|
"ns2": self.nameservers[1] if len(self.nameservers) > 1 else "",
|
||
|
"ip1": self.public_ip,
|
||
|
}
|
||
|
logging.info("Post data created")
|
||
|
return post_data
|