def stat_load(self): # execute uptime (exit_code,stdout,_) = self.do_execute( glob.config.get("paths", "uptime") ) # error with the uptime command if exit_code != 0: return errors.throw(errors.SERVER_UPTIME) # gather load averages stdout_arr = stdout[0].split(" ") load_average_1 = stdout_arr[-3].strip(",") load_average_5 = stdout_arr[-2].strip(",") load_average_15 = stdout_arr[-1].strip(",") # start PHP exits php_exit_first = False # update stats db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "load_average_1", load_average_1 ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # update stats db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "load_average_5", load_average_5 ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # update stats db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "load_average_15", load_average_15 ) # return code if php_exit_first: return php_exit_codes.translate(php_exit_first) else: return errors.throw(errors.ERR_SUCCESS)
def usage_disk(self): ret = super(ovz_core, self).usage_disk() if ret: return ret (exit_code,stdout,_) = self.do_execute( "%s exec %d \"/usr/bin/env df -B 1K -P\" | " % ( glob.config.get("paths", "vzctl"), self.real_id ) + \ "/usr/bin/env grep -E \" /$\" | " + \ "/usr/bin/env sed -r \"s/^[^ ]+[ ]+[^ ]+[ ]+([0-9]+) .*$/\\1/g\"" ) # command failed if exit_code != 0: return ovz_exit_codes.translate(exit_code) # update stats db (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "usage_disk", stdout[0].strip() ) # return code return php_exit_codes.translate(php_exit_code)
def users(self): ret = super(ovz_core, self).users() if ret: return ret (exit_code,stdout,_) = self.do_execute( "%s exec %d \"/usr/bin/env who -q\" | " % ( glob.config.get("paths", "vzctl"), self.real_id ) + \ "/usr/bin/env grep -vE \"^#\"" ) # command failed if exit_code != 0: return ovz_exit_codes.translate(exit_code) # update db (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "users", stdout[0].strip() ) # return code return php_exit_codes.translate(php_exit_code)
def stat_uptime(self): # execute uptime (exit_code,stdout,_) = self.do_execute( glob.config.get("paths", "uptime") ) # error with the uptime command if exit_code != 0: return errors.throw(errors.SERVER_UPTIME) # get uptime stdout_arr = stdout[0].split(",") uptime = stdout_arr[0][stdout_arr[0].index("up")+2:].strip() if uptime[-4:] == "days": uptime += " " + stdout_arr[1].strip() # update stats db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "uptime", uptime ) # return code return php_exit_codes.translate(php_exit_code)
def next_id(self): ret = super(ovz_core, self).next_id() if ret: return ret # real_id is fine try: self.real_id # need new real_id except AttributeError: ret = ovz_core.get_new_real_id(self) # fail if not type(ret) == int: return errors.throw(errors.OVZ_GENERATE_REAL_ID) # success self.real_id = ret # update DB (php_exit_code,_,_) = php.db_update( "vps", "update_real_id", str(self.vps_id), str(self.real_id) ) return php_exit_codes.translate(php_exit_code)
def uptime(self): ret = super(ovz_core, self).uptime() if ret: return ret (exit_code,stdout,_) = self.do_execute( "%s exec %d %s" % ( glob.config.get("paths", "vzctl"), self.real_id, glob.config.get("paths", "uptime") ) ) # error with the status command if exit_code != 0: return ovz_exit_codes.translate(exit_code) # get uptime stdout_arr = stdout[0].split(",") uptime = stdout_arr[0][stdout_arr[0].index("up")+2:].strip() if uptime[-4:] == "days": uptime += " " + stdout_arr[1].strip() # update stats db (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "uptime", uptime ) # return code return php_exit_codes.translate(php_exit_code)
def status(self): ret = super(ovz_core, self).status() if ret: return ret (exit_code,stdout,_) = self.do_execute( "%s status %d" % ( glob.config.get("paths", "vzctl"), self.real_id ) ) # error with the status command if exit_code != 0: return ovz_exit_codes.translate(exit_code) # split up stdout stdout_arr = stdout[0].split(" ") # deleted if stdout_arr[2] == "deleted": is_running = "0" ret = errors.VPS_STATUS_DELETED # running elif stdout[0][-9:] == " running\n": is_running = "1" ret = errors.VPS_STATUS_RUNNING # stopped else: is_running = "0" ret = errors.VPS_STATUS_STOPPED # update DB (php_exit_code,stdout,stderr) = php.db_update( "vps", "is_running", str(self.server['server_id']), str(self.vps_id), is_running ) # just report error, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) return ret
def stop(self): ret = super(ovz_core, self).stop() if ret: return ret (exit_code,_,_) = self.do_execute( "%s stop %d" % (glob.config.get("paths", "vzctl"), self.real_id) ) # update DB (php_exit_code,_,_) = php.db_update( "vps", "is_running", str(self.server['server_id']), str(self.vps_id), "0" ) # just report error, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) return ovz_exit_codes.translate(exit_code)
def unlock(self): # update PHP (php_exit_code,_,_) = php.db_update( "server", "is_locked", str(self.server_id), "0" ) # php exit code if php_exit_code != 0: return php_exit_codes.translate(php_exit_code) return errors.throw(errors.ERR_SUCCESS)
def __usage_cpu(self): # update db (php_exit_code, _, _) = php.db_update( "vps_stats", "update_attribute", str(self.server["server_id"]), str(self.vps_id), "usage_cpu_system", str(self.xentop_info[3]), # 3rd column is CPU ) return php_exit_codes.translate(php_exit_code)
def __usage_mem(self): # update db (php_exit_code, _, _) = php.db_update( "vps_stats", "update_attribute", str(self.server["server_id"]), str(self.vps_id), "usage_mem", str(self.xentop_info[4]), # 4th column is memory ) return php_exit_codes.translate(php_exit_code)
def stat_procs(self): # get cpuinfo (exit_code,cpuinfo,_) = self.do_execute( "/usr/bin/env cat /proc/cpuinfo" ) # cpuinfo error if exit_code != 0: return errors.throw(errors.SERVER_CPUINFO) # update stats db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "cpuinfo", "\n".join(cpuinfo) ) # return code return php_exit_codes.translate(php_exit_code)
def usage_mem(self): ret = super(ovz_core, self).usage_mem() if ret: return ret (exit_code,stdout,_) = self.do_execute( "%s exec %d \"/usr/bin/env cat /proc/meminfo\"" % ( glob.config.get("paths", "vzctl"), self.real_id ) ) # command failed if exit_code != 0: return ovz_exit_codes.translate(exit_code) # get string version of stdout str_stdout = " ".join(stdout) # get total match = re.search("MemTotal:[ ]*([0-9]+)", str_stdout) total = int(match.groups()[0]) # get free match = re.search("MemFree:[ ]*([0-9]+)", str_stdout) free = int(match.groups()[0]) # get cached match = re.search("Cached:[ ]*([0-9]+)", str_stdout) cache = int(match.groups()[0]) # calculate usage usage = total - free - cache # update db (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "usage_mem", str(usage) ) # return code return php_exit_codes.translate(php_exit_code)
def uptime(self): ret = super(xen_core, self).uptime() if ret: return ret # get uptime (exit_code, stdout, _) = self.do_execute("%s uptime %d" % (glob.config.get("paths", "xm"), self.real_id)) if exit_code != 0: return xen_exit_codes.translate(exit_code) # parse uptime uptime_split = re.sub("[ ]+", " ", stdout[1].strip()).split(" ") uptime_split = uptime_split[2:] uptime = " ".join(uptime_split) # update db (php_exit_code, _, _) = php.db_update( "vps_stats", "update_attribute", str(self.server["server_id"]), str(self.vps_id), "uptime", str(uptime) ) return php_exit_codes.translate(php_exit_code)
def usage_beancounters(self): if not self.require("real_id"): return errors.throw(errors.BACKEND_INVALID_INPUT) (exit_code,beancounters,_) = self.do_execute( "%s exec %d cat /proc/user_beancounters" % ( glob.config.get("paths", "vzctl"), self.real_id ) ) if exit_code != 0: return ovz_exit_codes.translate(exit_code) # update db (php_exit_code,tmp,tmp2) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "usage_beancounters", "\n".join(beancounters) ) # return code return php_exit_codes.translate(php_exit_code)
def usage_disk(self): ret = super(xen_core, self).usage_disk() if ret: return ret (exit_code, stdout, _) = self.do_execute( 'du "%s"' % os.path.join(glob.config.get("xen", "root_domain_dir"), str(self.real_id), "sda1.img") ) # calculate space used_space = stdout[0].split("\t")[0] # update db (php_exit_code, _, _) = php.db_update( "vps_stats", "update_attribute", str(self.server["server_id"]), str(self.vps_id), "usage_disk", used_space, # 3rd column is CPU ) return php_exit_codes.translate(php_exit_code)
def usage_cpu(self): ret = super(ovz_core, self).usage_cpu() if ret: return ret (exit_code,stdout,_) = self.do_execute( "%s exec %d \"%s\" | " % ( glob.config.get("paths", "vzctl"), self.real_id, glob.config.get("paths", "vmstat") ) + \ "/usr/bin/env sed -r \"s/[ ]+/ /g\"" ) # command failed if exit_code != 0: return ovz_exit_codes.translate(exit_code) # parse out CPU info data_line = stdout[2] data_arr = data_line.split(" ") usage_cpu_user = data_arr[-5] usage_cpu_system = data_arr[-4] usage_cpu_idle = data_arr[-3] # start PHP exits php_exit_first = False # user CPU usage (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "usage_cpu_user", usage_cpu_user ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # system CPU usage (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "usage_cpu_system", usage_cpu_system ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # idle CPU usage (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "usage_cpu_idle", usage_cpu_idle ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # return code if php_exit_first: return php_exit_codes.translate(php_exit_first) else: return errors.throw(errors.ERR_SUCCESS)
def stat_mem(self): # get meminfo (exit_code,stdout,_) = self.do_execute( "/usr/bin/env cat /proc/meminfo" ) # meminfo error if exit_code != 0: return errors.throw(errors.SERVER_MEMINFO) # get string version of stdout str_stdout = " ".join(stdout) # get total match = re.search("MemTotal:[ ]*([0-9]+)", str_stdout) total = int(match.groups()[0]) # get free match = re.search("MemFree:[ ]*([0-9]+)", str_stdout) free = int(match.groups()[0]) # get cached memory match = re.search("Cached:[ ]*([0-9]+)", str_stdout) cache = int(match.groups()[0]) # calculate usage usage = total - free - cache # start PHP exits php_exit_first = False # update db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "usage_mem", str(usage) ) # just throw, DO NOT RETURN if exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # update db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "max_mem", str(total) ) # just throw, DO NOT RETURN if exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # return code if php_exit_first: return php_exit_codes.translate(php_exit_first) else: return errors.throw(errors.ERR_SUCCESS)
def stat_disk(self): # get disk name (exit_code,disk_name,_) = self.do_execute( "/usr/bin/env df -B 1K -P | " + "/usr/bin/env grep -E \" /$\" | " + "/usr/bin/env cut -d ' ' -f1 | " + "/usr/bin/env sed -r \"s/[0-9]+$//g\"" ) # just the first line disk_name = disk_name[0].strip() # disk usage error if exit_code != 0: return errors.throw(errors.SERVER_DISKUSAGE) # get disk usage (exit_code,disk_info,_) = self.do_execute( "/usr/bin/env df -B 1K -P | " + "/usr/bin/env grep \"%s\" | " % disk_name + "/usr/bin/env sed -r \"s/[ ]+/ /g\"" ) # disk usage error if exit_code != 0: return errors.throw(errors.SERVER_DISKUSAGE) # get usage and total usage = 0 total = 0 for info in disk_info: disk_info_arr = info.split(" ") usage += int(disk_info_arr[2]) total += int(disk_info_arr[3]) # start PHP exits php_exit_first = False # update db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "usage_disk", str(usage) ) # just throw, DO NOT RETURN if exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # update db (php_exit_code,_,_) = php.db_update( "server_stats", "update_attribute", str(self.get_server_id()), "max_disk", str(total) ) # just throw, DO NOT RETURN if exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # return code if php_exit_first: return php_exit_codes.translate(php_exit_first) else: return errors.throw(errors.ERR_SUCCESS)
def load_averages(self): ret = super(ovz_core, self).load_averages() if ret: return ret (exit_code,stdout,_) = self.do_execute( "%s exec %d %s" % ( glob.config.get("paths", "vzctl"), self.real_id, glob.config.get("paths", "uptime") ) ) # error with the status command if exit_code != 0: return ovz_exit_codes.translate(exit_code) # gather load averages stdout_arr = stdout[0].split(" ") load_average_1 = stdout_arr[-3].strip(",") load_average_5 = stdout_arr[-2].strip(",") load_average_15 = stdout_arr[-1].strip(",").strip() # start PHP exits php_exit_first = False # update stats db (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "load_average_1", load_average_1 ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # update stats db (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "load_average_5", load_average_5 ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # update stats db (php_exit_code,_,_) = php.db_update( "vps_stats", "update_attribute", str(self.server['server_id']), str(self.vps_id), "load_average_15", load_average_15 ) # just throw, DO NOT RETURN if php_exit_code != 0: php_exit_codes.translate(php_exit_code) if not php_exit_first: php_exit_first = php_exit_code # return code if php_exit_first: return php_exit_codes.translate(php_exit_first) else: return errors.throw(errors.ERR_SUCCESS)
def ost_create(self): ret = super(ovz_core, self).ost_create() if ret: return ret # stop VM (exit_code,_,_) = self.do_execute( "%s stop %d" % ( glob.config.get("paths", "vzctl"), self.real_id ) ) # also must account for not running ret = ovz_exit_codes.translate(exit_code) if exit_code != 0 and ret != errors.OVZ_NOT_RUNNING: return ret # sanitize OST path self.do_execute("mkdir -p \"%s\"" % glob.config.get("paths", "ost_ovz")) # get OST path and private directories ost_path = os.path.join( glob.config.get("paths", "ost_ovz"), executer.escape(self.ost_file) ) ost_private = os.path.join( glob.config.get("paths", "ovz_private"), str(self.real_id) ) # sanity checks self.do_execute( "rm -f \"%s\" \"%s\"" % (ost_path + ".tar", ost_path + ".tar.gz") ) # get list of directories, then tar it (exit_code,_,_) = self.do_execute( "ls -x \"%s\" | /usr/bin/env xargs " % ost_private + "/usr/bin/env tar cf \"%s\" -C \"%s\"" % ( ost_path + ".tar", ost_private ) ) if exit_code != 0: return errors.throw(ERR_TAR_FAILED) # gzip tarred VM (exit_code,_,_) = self.do_execute( "/usr/bin/env gzip \"%s\"" % (ost_path + ".tar") ) if exit_code != 0: return errors.throw(ERR_GZIP_FAILED) # transfer OST over to master node (exit_code,_,_) = execute( "scp -i \"%s\" -P %d \"root@%s:%s\" \"%s\"" % ( glob.config.get("paths", "master_private_key"), self.server['port'], executer.escape(self.server['ip']), ost_path + ".tar.gz", ost_path + ".tar.gz" ) ) if exit_code != 0: return errors.throw(ERR_SCP_FAILED) # update DB (php_exit_code,_,_) = php.db_update( "ost", "insert", self.ost_name, ost_path + ".tar.gz", self.ost_driver_id, self.ost_arch ) if php_exit_code != 0: return php_exit_codes.translate(php_exit_code) # start VM (exit_code,_,_) = self.do_execute( "%s start %d" % ( glob.config.get("paths", "vzctl"), self.real_id ) ) return ovz_exit_codes.translate(exit_code)
def ost_create(self): ret = super(xen_core, self).ost_create() if ret: return ret # stop VM error_code = self.stop() if error_code != errors.ERR_SUCCESS: return error_code # disk path disk_img_path = self.__get_disk_img_path() # mount loopback file loop_dir = self.__mount_loopback(disk_img_path) if not loop_dir: self.__quit_clean() return errors.throw(errors.XEN_MOUNT_LOOP) # sanitize OST path self.do_execute('mkdir -p "%s"' % glob.config.get("paths", "ost_xen")) # get OST path and private directories ost_path = self.__get_ost_path(self.ost_file) # sanity checks self.do_execute('rm -f "%s" "%s"' % (ost_path + ".tar", ost_path + ".tar.gz")) # get list of directories, then tar it (exit_code, _, _) = self.do_execute( 'ls -1 "%s" | xargs tar cf "%s" -C "%s"' % (loop_dir, ost_path + ".tar", loop_dir) ) if exit_code != 0: self.__quit_clean(loop_dir) return errors.throw(errors.ERR_TAR_FAILED) # gzip tarred VM (exit_code, _, _) = self.do_execute('/usr/bin/env gzip "%s"' % (ost_path + ".tar")) if exit_code != 0: self.__quit_clean(loop_dir) return errors.throw(errors.ERR_GZIP_FAILED) # transfer OST over to master node (exit_code, _, _) = execute( 'scp -i "%s" -P %d "root@%s:%s" "%s"' % ( glob.config.get("paths", "master_private_key"), self.server["port"], executer.escape(self.server["ip"]), ost_path + ".tar.gz", ost_path + ".tar.gz", ) ) if exit_code != 0: self.__quit_clean(loop_dir) return errors.throw(errors.ERR_SCP_FAILED) # update DB (php_exit_code, _, _) = php.db_update( "ost", "insert", self.ost_name, ost_path + ".tar.gz", self.ost_driver_id, self.ost_arch ) if php_exit_code != 0: self.__quit_clean(loop_dir) return php_exit_codes.translate(php_exit_code) # clean up self.__quit_clean(loop_dir) # start it back up exit_status = self.start() # ignore start errors return errors.throw(errors.ERR_SUCCESS)
def usage_bandwidth(self): if not self.require("real_id"): return errors.throw(errors.BACKEND_INVALID_INPUT) # vps IP addresses ac = api.api_call("vm_get_info", { 'vps_id': self.vps_id }) ret = ac.execute() if ret != errors.ERR_SUCCESS: return ret result = ac.output() # check output try: self.ips = result[0]['ip'] # error with output except IndexError: return errors.throw(errors.BACKEND_ERR_UNKNOWN) # no IPs associated with it, so nothing to update except KeyError: return errors.throw(errors.ERR_SUCCESS) # get IP stats total_destination = 0 total_source = 0 for ip in self.ips: (exit_code,stdout,_) = self.do_execute( "%s -nvxL PANENTHE_BW | /usr/bin/env grep \"%s\" | " % ( glob.config.get("paths", "iptables"), ip ) + "/usr/bin/env sed -r \"s/[ ]+/ /g\"" ) for line in stdout: data = line.split(" ") # 2 is bytes, 7 is source IP, 8 is destination IP if data[7] == "0.0.0.0/0" and data[8] != "0.0.0.0/0": total_destination += int(data[2]) elif data[8] == "0.0.0.0/0" and data[7] != "0.0.0.0/0": total_source += int(data[2]) # update PHP (php_exit_code,_,_) = php.db_update( "vps_stats", "update_bandwidth", str(self.server['server_id']), str(self.vps_id), str(total_destination), str(total_source) ) # php exit code if php_exit_code != 0: return php_exit_codes.translate(php_exit_code) # set up iptables for the rules since PHP was updated successfully for ip in self.ips: # add #1 (exit_code,_,_) = iptables.add_rule( "PANENTHE_BW", "-d %s" % ip, self.do_execute ) if exit_code != 0: return errors.throw(errors.SERVER_IPTABLES) # add #2 (exit_code,_,_) = iptables.add_rule( "PANENTHE_BW", "-s %s" % ip, self.do_execute ) if exit_code != 0: return errors.throw(errors.SERVER_IPTABLES) return errors.throw(errors.ERR_SUCCESS)