def _install_psutils(): _install_gcc() _install_pip() if waagent.Run("pip install psutil", chk_err=False) == 0: waagent.Log("psutil installed") else: waagent.Log("Error installing psutil")
def install_package(package_name): if DistroName == "centos" or DistroName == "redhat": cmd = "yum -y install " + package_name elif DistroName == "ubuntu": cmd = "apt-get -y install " + package_name elif DistroName == "suse": if not os.listdir('/etc/zypp/repos.d'): waagent.Run("zypper ar http://download.opensuse.org/distribution/13.2/repo/oss/suse/ opensuse") cmd = "zypper -n --gpg-auto-import-keys install --force-resolution -l " + package_name else: cmd = "zypper -n install --force-resolution -l " + package_name else: raise Exception("Unsupported Linux Distro.") waagent.Log("The command to install {0}: {1}".format(package_name, cmd)) attempt = 1 while(True): waagent.Log("Installing package {0} (Attempt {1})".format(package_name, attempt)) retcode, retoutput = waagent.RunGetOutput(cmd) if retcode == 0: waagent.Log("package {0} installation succeeded".format(package_name)) break else: waagent.Log("package {0} installation failed {1}:\n {2}".format(package_name, retcode, retoutput)) if attempt < 3: attempt += 1 time.sleep(5) if DistroName == 'suse' and retcode == 104: waagent.Run("zypper ar http://download.opensuse.org/distribution/13.2/repo/oss/suse/ opensuse") cmd = "zypper -n --gpg-auto-import-keys install --force-resolution -l " + package_name continue else: raise Exception("failed to install package {0}:{1}".format(package_name, retcode))
def enable(hutil): pidFile = os.path.join(aem.LibDir, "pid"); #Check whether monitor process is running. #If it does, return. Otherwise clear pid file if os.path.isfile(pidFile): pid = waagent.GetFileContents(pidFile) if os.path.isdir(os.path.join("/proc", pid)): if hutil.is_seq_smaller(): hutil.do_exit(0, 'Enable', 'success', '0', 'Azure Enhanced Monitor is already running') else: waagent.Log("Stop old daemon: {0}".format(pid)) os.kill(int(pid), 9) os.remove(pidFile) args = [os.path.join(os.getcwd(), __file__), "daemon"] devnull = open(os.devnull, 'w') child = subprocess.Popen(args, stdout=devnull, stderr=devnull) if child.pid == None or child.pid < 1: hutil.do_exit(1, 'Enable', 'error', '1', 'Failed to launch Azure Enhanced Monitor') else: hutil.save_seq() waagent.SetFileContents(pidFile, str(child.pid)) waagent.Log(("Daemon pid: {0}").format(child.pid)) hutil.do_exit(0, 'Enable', 'success', '0', 'Azure Enhanced Monitor is enabled')
def init_suse_hostsfile(host_name, ipaddrs): hostsfile = '/etc/hosts' if not os.path.isfile(hostsfile): return try: newhpcd_entries = '' for ipaddr in ipaddrs: newhpcd_entries += '{0:24}{1:30}#HPCD\n'.format(ipaddr, host_name) curhpcd_entries = '' newcontent = '' hpcentryexists = False with open(hostsfile, 'r') as F: for line in F.readlines(): if re.match(r"^[0-9\.]+\s+[^\s#]+\s+#HPCD\s*$", line): curhpcd_entries += line hpcentryexists = True elif re.match(r"^[0-9\.]+\s+[^\s#]+\s+#HPC\s*$", line): hpcentryexists = True else: newcontent += line if newhpcd_entries != curhpcd_entries: if hpcentryexists: waagent.Log("Clean the HPC related host entries from hosts file") waagent.Log("Add the following HPCD host entries:\n{0}".format(newhpcd_entries)) if newcontent and newcontent[-1] != '\n': newcontent += '\n' newcontent += newhpcd_entries waagent.ReplaceFileContentsAtomic(hostsfile,newcontent) os.chmod(hostsfile, 0o644) except : raise
def daemon(hutil): publicConfig = hutil.get_public_settings() privateConfig = hutil.get_protected_settings() config = aem.EnhancedMonitorConfig(publicConfig, privateConfig) monitor = aem.EnhancedMonitor(config) hutil.set_verbose_log(config.isVerbose()) InitExtensionEventLog(hutil.get_name()) while True: waagent.Log("Collecting performance counter.") startTime = time.time() try: monitor.run() message = ("deploymentId={0} roleInstance={1} OK" "").format(config.getVmDeploymentId(), config.getVmRoleInstance()) hutil.do_status_report("Enable", "success", 0, message) except Exception as e: waagent.Error("{0} {1}".format(printable(e), traceback.format_exc())) hutil.do_status_report("Enable", "error", 0, "{0}".format(e)) waagent.Log("Finished collection.") timeElapsed = time.time() - startTime timeToWait = (aem.MonitoringInterval - timeElapsed) #Make sure timeToWait is in the range [0, aem.MonitoringInterval) timeToWait = timeToWait % aem.MonitoringInterval time.sleep(timeToWait)
def _check_and_install_package(pkg, cmd = None): if not cmd: cmd = pkg if waagent.Run("command -v {0}".format(cmd), chk_err=False) == 0: waagent.Log("{0} was already installed".format(pkg)) else: waagent.Log("Start to install {0}".format(pkg)) install_package(pkg) waagent.Log("{0} was successfully installed".format(pkg))
def _is_nodemanager_daemon(pid): retcode, output = waagent.RunGetOutput("ps -p {0} -o cmd=".format(pid)) if retcode == 0: waagent.Log("The cmd for process {0} is {1}".format(pid, output)) pattern = r'(.*[/\s])?{0}\s+[-/]*daemon$'.format(os.path.basename(__file__)) if re.match(pattern, output): return True waagent.Log("The process {0} is not HPC Linux node manager daemon".format(pid)) return False
def _install_pip(): ec = waagent.Run("curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py --tlsv1.2", chk_err=False) if ec != 0: waagent.Log("get-pip.py was not downloaded, {0}".format(ec)) raise Exception("failed to install package python-pip:{0}".format(ec)) waagent.Log("get-pip.py was downloaded") ec = waagent.Run("python get-pip.py", chk_err=False) if ec != 0: waagent.Log("get-pip.py run failed, {0}".format(ec)) raise Exception("failed to install package python-pip:{0}".format(ec)) waagent.Log("python-pip was installed")
def write(self, counters, maxRetry = 3, eventFile=EventFile): for i in range(0, maxRetry): try: self._write(counters, eventFile) waagent.Log(("Write {0} counters to event file." "").format(len(counters))) return except IOError, e: waagent.Warn(("Write to perf counters file failed: {0}" "").format(printable(e))) waagent.Log("Retry: {0}".format(i)) time.sleep(1)
def _mount_cgroup(): if not os.path.isdir('/cgroup'): os.mkdir('/cgroup') if not os.listdir('/cgroup'): retcode, mount_msg = waagent.RunGetOutput('mount -t cgroup cgroup /cgroup') waagent.Log("mount /cgroup directory {0}:{1}".format(retcode, mount_msg)) if retcode == 0: waagent.Log("/cgroup directory is successfully mounted.") else: raise Exception("failed to mount /cgroup directory") else: waagent.Log("/cgroup directory was already mounted.")
def _install_cgroup_tool(): if waagent.Run("command -v cgexec", chk_err=False) == 0: waagent.Log("cgroup tools was already installed") else: waagent.Log("Start to install cgroup tools") if DistroName == "ubuntu": cg_pkgname = 'cgroup-bin' elif (DistroName == "centos" or DistroName == "redhat") and re.match("^6", DistroVersion): cg_pkgname = 'libcgroup' else: cg_pkgname = 'libcgroup-tools' install_package(cg_pkgname) waagent.Log("cgroup tool was successfully installed")
def _add_dns_search(domain_fqdn): need_update = False new_content = '' for line in (open('/etc/resolv.conf','r')).readlines(): if re.match('^search.* {0}'.format(domain_fqdn), line): waagent.Log('{0} was already added in /etc/resolv.conf'.format(domain_fqdn)) return if re.match('^search', line): need_update = True new_content += line.replace('search', 'search {0}'.format(domain_fqdn)) else: new_content += line if need_update: waagent.Log('Adding {0} to /etc/resolv.conf'.format(domain_fqdn)) waagent.SetFileContents('/etc/resolv.conf', new_content)
def setUp(self): print( '\n\n============================================================================================' ) waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." % (ExtensionShortName)) global hutil hutil = Util.HandlerUtility(waagent.Log, waagent.Error) hutil.do_parse_context('TEST') global MyPatching MyPatching = FakePatching(hutil) if MyPatching is None: sys.exit(1) distro = DistInfo()[0] if 'centos' in distro or 'Oracle' in distro or 'redhat' in distro: MyPatching.cron_restart_cmd = 'service crond restart' try: os.remove('mrseq') except: pass waagent.SetFileContents(MyPatching.package_downloaded_path, '') waagent.SetFileContents(MyPatching.package_patched_path, '')
def main(): #Global Variables definition waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." % (ExtensionShortName)) hutil = None try: for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): dummy_command("Disable", "success", "Disable succeeded") elif re.match("^([-/]*)(uninstall)", a): dummy_command("Uninstall", "success", "Uninstall succeeded") elif re.match("^([-/]*)(install)", a): dummy_command("Install", "success", "Install succeeded") elif re.match("^([-/]*)(enable)", a): hutil = parse_context("Enable") enable(hutil) elif re.match("^([-/]*)(daemon)", a): hutil = parse_context("Executing") daemon(hutil) elif re.match("^([-/]*)(update)", a): dummy_command("Update", "success", "Update succeeded") except Exception as e: err_msg = "Failed with error: {0}, {1}".format(e, traceback.format_exc()) waagent.Error(err_msg) if hutil is not None: hutil.error(err_msg) hutil.do_exit(1, 'Enable', 'failed', '0', 'Enable failed: {0}'.format(err_msg))
def main(): waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." %(ExtensionShortName)) global hutil hutil = Util.HandlerUtility(waagent.Log, waagent.Error, ExtensionShortName) global MyPatching MyPatching = GetMyPatching(hutil) if MyPatching is None: sys.exit(1) for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): disable() elif re.match("^([-/]*)(uninstall)", a): uninstall() elif re.match("^([-/]*)(install)", a): install() elif re.match("^([-/]*)(enable)", a): enable() elif re.match("^([-/]*)(update)", a): update() elif re.match("^([-/]*)(download)", a): download() elif re.match("^([-/]*)(patch)", a): patch() elif re.match("^([-/]*)(oneoff)", a): oneoff()
def main(): if len(sys.argv) == 1: unittest.main() return waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." % (ExtensionShortName)) global hutil hutil = Util.HandlerUtility(waagent.Log, waagent.Error) hutil.do_parse_context('TEST') global MyPatching MyPatching = FakePatching(hutil) if MyPatching == None: sys.exit(1) for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): disable() elif re.match("^([-/]*)(uninstall)", a): uninstall() elif re.match("^([-/]*)(install)", a): install() elif re.match("^([-/]*)(enable)", a): enable() elif re.match("^([-/]*)(update)", a): update() elif re.match("^([-/]*)(download)", a): download() elif re.match("^([-/]*)(patch)", a): patch() elif re.match("^([-/]*)(oneoff)", a): oneoff()
def main(): waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." % (ExtensionShortName)) global hutil hutil = Util.HandlerUtility(waagent.Log, waagent.Error, ExtensionShortName) global myProvision myProvision = GetMyProvision(hutil) if myProvision == None: sys.exit(1) if os.path.isfile("mrseq"): os.remove("mrseq") for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): disable() elif re.match("^([-/]*)(uninstall)", a): uninstall() elif re.match("^([-/]*)(install)", a): install() elif re.match("^([-/]*)(enable)", a): enable() elif re.match("^([-/]*)(update)", a): update() elif re.match("^([-/]*)(provision)", a): provision()
def main(): waagent.LoggerInit('/var/log/waagent.log','/dev/stdout') waagent.Log("%s started to handle." %(ExtensionShortName)) global hutil hutil = Util.HandlerUtility(waagent.Log, waagent.Error) hutil.try_parse_context() global public_settings public_settings = hutil.get_public_settings() if not public_settings: waagent.AddExtensionEvent(name=ExtensionShortName, op='MainInProgress', isSuccess=True, message="Public settings are NOT provided.") public_settings = {} global protected_settings protected_settings = hutil.get_protected_settings() if not protected_settings: waagent.AddExtensionEvent(name=ExtensionShortName, op='MainInProgress', isSuccess=True, message="protected settings are NOT provided.") protected_settings = {} global distro_category distro_category = get_distro_category() for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): disable() elif re.match("^([-/]*)(uninstall)", a): uninstall() elif re.match("^([-/]*)(install)", a): install() elif re.match("^([-/]*)(enable)", a): enable() elif re.match("^([-/]*)(update)", a): update()
def init_globals(): """Initialize all the globals in a function so that we can catch any exceptions that might be raised.""" global hutil, WorkDir, MDSDFileResourcesDir, MDSDRoleName, MDSDFileResourcesPrefix, MDSDPidFile, MDSDPidPortFile global EnableSyslog, ExtensionOperationType, MdsdFolder, StartDaemonFilePath, MDSD_LISTEN_PORT, omfileconfig # global rsyslog_ommodule_for_check, RunGetOutput, MdsdFolder, omi_universal_pkg_name # global DebianConfig, RedhatConfig, UbuntuConfig1510OrHigher, SUSE11_MDSD_SSL_CERTS_FILE # global SuseConfig11, SuseConfig12, CentosConfig, All_Dist waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("LinuxAzureDiagnostic started to handle.") hutil = Util.HandlerUtility(waagent.Log, waagent.Error) init_distro_specific_actions() WorkDir = os.getcwd() MDSDFileResourcesDir = "/var/run/mdsd" MDSDRoleName = 'lad_mdsd' MDSDFileResourcesPrefix = os.path.join(MDSDFileResourcesDir, MDSDRoleName) MDSDPidFile = os.path.join(WorkDir, 'mdsd.pid') MDSDPidPortFile = MDSDFileResourcesPrefix + '.pidport' EnableSyslog = True ExtensionOperationType = None MdsdFolder = os.path.join(WorkDir, 'bin') StartDaemonFilePath = os.path.join(os.getcwd(), __file__) MDSD_LISTEN_PORT = 29131 omfileconfig = os.path.join(WorkDir, 'omfileconfig') init_public_and_private_settings()
def main(): waagent.LoggerInit('/var/log/waagent.log','/dev/stdout') waagent.Log("%s started to handle." %(ExtensionShortName)) global hutil hutil = Util.HandlerUtility(waagent.Log, waagent.Error, ExtensionShortName) hutil.try_parse_context() global public_settings public_settings = hutil.get_public_settings() if not public_settings: public_settings = {} global protected_settings protected_settings = hutil.get_protected_settings() if not protected_settings: protected_settings = {} global distro_category distro_category = get_distro_category() for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): disable() elif re.match("^([-/]*)(uninstall)", a): uninstall() elif re.match("^([-/]*)(install)", a): install() elif re.match("^([-/]*)(enable)", a): enable() elif re.match("^([-/]*)(update)", a): update()
def main(): waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout', True) waagent.Log("%s started to handle." % (ExtensionShortName)) try: for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): hutil = parse_context("Disable") disable(hutil) elif re.match("^([-/]*)(uninstall)", a): hutil = parse_context("Uninstall") uninstall(hutil) elif re.match("^([-/]*)(install)", a): hutil = parse_context("Install") install(hutil) elif re.match("^([-/]*)(enable)", a): hutil = parse_context("Enable") enable(hutil) elif re.match("^([-/]*)(update)", a): dummy_command("Update", "success", "Update succeeded") except Exception as e: err_msg = ("Failed to enable the extension with error: {0}, " "{1}").format(e, traceback.format_exc()) waagent.Error(err_msg) hutil.error(err_msg) hutil.do_exit(1, 'Enable', 'failed', '0', 'Enable failed: {0}'.format(e))
def main(): waagent.LoggerInit('/var/log/waagent.log','/dev/stdout') waagent.Log("{0} started to handle.".format(ExtensionShortName)) if not os.path.isdir(aem.LibDir): os.makedirs(aem.LibDir) for command in sys.argv[1:]: if re.match("^([-/]*)(install)", command): grace_exit("install", "success", "Install succeeded") if re.match("^([-/]*)(uninstall)", command): grace_exit("uninstall", "success", "Uninstall succeeded") if re.match("^([-/]*)(update)", command): grace_exit("update", "success", "Update succeeded") try: if re.match("^([-/]*)(enable)", command): hutil = parse_context("enable") enable(hutil) elif re.match("^([-/]*)(disable)", command): hutil = parse_context("disable") disable(hutil) elif re.match("^([-/]*)(daemon)", command): hutil = parse_context("enable") daemon(hutil) except Exception as e: hutil.error("{0}, {1}".format(e, traceback.format_exc())) hutil.do_exit(1, command, 'failed','0', '{0} failed:{1}'.format(command, e))
def waagent_log_info(message): """ Log informational message, being cautious of possibility that waagent may not be imported """ if 'Utils.WAAgentUtil' in sys.modules: waagent.Log(message) else: print('Info: {0}'.format(message))
def getStorageMetrics(account, key, hostBase, table, startKey, endKey): try: waagent.Log("Retrieve storage metrics data.") tableService = TableService(account_name = account, account_key = key, host_base = hostBase) ofilter = ("PartitionKey ge '{0}' and PartitionKey lt '{1}'" "").format(startKey, endKey) oselect = ("TotalRequests,TotalIngress,TotalEgress,AverageE2ELatency," "AverageServerLatency,RowKey") metrics = tableService.query_entities(table, ofilter, oselect) waagent.Log("{0} records returned.".format(len(metrics))) return metrics except Exception, e: waagent.Error(("Failed to retrieve storage metrics data: {0} {1}" "").format(printable(e), traceback.format_exc())) AddExtensionEvent(message=FAILED_TO_RETRIEVE_STORAGE_DATA) return None
def main(): waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." % ExtensionShortName) operation = "disable" status = "success" msg = "Disabled successfully." hutil = parse_context(operation) hutil.log("Start to disable.") hutil.log(msg) hutil.do_exit(0, operation, status, '0', msg)
def main(): #Global Variables definition waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." % (ExtensionShortName)) operation = "update" status = "success" msg = "Updated successfully." hutil = parse_context(operation) hutil.log("Start to update.") hutil.log(msg) hutil.do_exit(0, operation, status, '0', msg)
def disable(): waagent.Log("disable() called.") hutil = parse_context('Disable') #TODO where to kill the node manager #Check whether monitor process is running. #If it does, kill it. Otherwise clear pid file if os.path.isfile(DaemonPidFilePath): pid = waagent.GetFileContents(DaemonPidFilePath) waagent.Log("Discovered daemon pid: {0}".format(pid)) if os.path.isdir(os.path.join("/proc", pid)) and _is_nodemanager_daemon(pid): waagent.Log(("Stop HPC node manager daemon: {0}").format(pid)) os.killpg(int(pid), 9) cleanup_host_entries() hutil.do_exit(0, 'Disable', 'success', '0', 'HPC node manager daemon is disabled') waagent.Log("Remove the daemon pid file: {0}".format(DaemonPidFilePath)) os.remove(DaemonPidFilePath) else: waagent.Log("No daemon pid file discovered.") hutil.do_exit(0, 'Disable', 'success', '0', 'HPC node manager daemon disabled')
def _update_dns_record(domain_fqdn): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while True: try: s.connect((domain_fqdn, 53)) break except Exception as e: waagent.Log('Failed to connect to {0}:53: {1}'.format(domain_fqdn, e)) ipaddr = s.getsockname()[0] host_fqdn = "{0}.{1}".format(socket.gethostname().split('.')[0], domain_fqdn) dns_cmd = 'echo -e "server {0}\nzone {0}\nupdate delete {1}\nupdate add {1} 864000 A {2}\nsend\n" | nsupdate -v'.format(domain_fqdn, host_fqdn, ipaddr) waagent.Log('The command to update ip to dns server is: {0}'.format(dns_cmd)) retry = 0 while retry < 60: dns_ret, dns_msg = waagent.RunGetOutput(dns_cmd) if dns_ret == 0: waagent.Log("Succeeded to update ip to dns server.") return else: retry = retry + 1 waagent.Log("Failed to update ip to dns server: {0}, {1}".format(dns_ret, dns_msg)) time.sleep(10)
def main(): waagent.LoggerInit('/var/log/waagent.log', '/dev/stdout') waagent.Log("%s started to handle." % (ExtensionShortName)) for a in sys.argv[1:]: if re.match("^([-/]*)(disable)", a): disable() elif re.match("^([-/]*)(uninstall)", a): uninstall() elif re.match("^([-/]*)(install)", a): install() elif re.match("^([-/]*)(enable)", a): enable() elif re.match("^([-/]*)(update)", a): update()
def disable(hutil): pidFile = os.path.join(aem.LibDir, "pid"); #Check whether monitor process is running. #If it does, kill it. Otherwise clear pid file if os.path.isfile(pidFile): pid = waagent.GetFileContents(pidFile) if os.path.isdir(os.path.join("/proc", pid)): waagent.Log(("Stop daemon: {0}").format(pid)) os.kill(int(pid), 9) os.remove(pidFile) hutil.do_exit(0, 'Disable', 'success', '0', 'Azure Enhanced Monitor is disabled') os.remove(pidFile) hutil.do_exit(0, 'Disable', 'success', '0', 'Azure Enhanced Monitor is not running')