• #!/usr/bin/env python”””Licensed to the Apache Software Foundation (ASF) under oneor more contributor license agreements. See the NOTICE filedistributed with this work for additional informationregarding copyright ownership. The ASF licenses this fileto you under the Apache License, Version 2.0 (the”License”); you may not use this file except in compliancewith the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an “AS IS” BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.”””import sys
  • import os
  • import glob
  • import pwd
  • import grp
  • import signal
  • import time
  • from resource_management import *
  • #from prometheus_common import kill_processclass Master(Script):
  • # Install prometheussearch def install(self, env):
  • # Import properties defined in -config.xml file from the params class import params
  • # This allows us to access the params.prometheus_pid_file property as # format(‘{prometheus_pid_file}’) env.set_params(params)
  • # Install dependent packages self.install_packages(env)
  • # Create user and group for prometheus if they don’t exist try:
  • grp.getgrnam(params.prometheus_group)
  • except KeyError:
  • Group(group_name=params.prometheus_group)
  • try:
  • pwd.getpwnam(params.prometheus_user)
  • except KeyError:
  • User(username=params.prometheus_user, gid=params.prometheus_group, groups=[params.prometheus_group], ignore_failures=True )
  • # Create prometheus directories Directory([params.prometheus_base_dir, params.prometheus_pid_dir], mode=0755, cd_access=’a’, owner=params.prometheus_user, group=params.prometheus_group, create_parents=True )
  • # Download prometheussearch cmd = format(“cp /home/zyy/prometheus-2.34.0.linux-amd64.tar.gz {prometheus_base_dir}/prometheus-2.34.0.tar.gz”)
  • Execute(cmd, user=”root”)
  • cmd1 = format(“cd {prometheus_base_dir}; tar -zvxf prometheus-2.34.0.tar.gz”)
  • Execute(cmd1, user=”root”)
  • cmd2 = format(“cd {prometheus_base_dir}; rm prometheus-2.34.0.tar.gz”)
  • Execute(cmd2, user=params.prometheus_user)
  • Execute(‘echo “Install complete”‘)
  • def configure(self, env):
  • # Import properties defined in -config.xml file from the params class import params
  • # This allows us to access the params.prometheus_pid_file property as # format(‘{prometheus_pid_file}’) env.set_params(params)
  • Execute(‘echo “Configuration complete”‘)
  • def stop(self, env):
  • # Import properties defined in -config.xml file from the params class import params
  • # Import properties defined in -env.xml file from the status_params class import status_params
  • # This allows us to access the params.prometheus_pid_file property as # format(‘{prometheus_pid_file}’) env.set_params(params)
  • # Stop prometheussearch #kill_process(params.prometheus_pid_file, user=”root”) cmd = format(“systemctl stop prometheus.service”)
  • Execute(cmd, user=”root”)
  • # cmd = format(“kill `cat {prometheus_pid_file}`”) # Execute(cmd, user=params.prometheus_user, only_if=format(“test -f {prometheus_pid_file}”)) def start(self, env):
  • # Import properties defined in -config.xml file from the params class import params
  • # This allows us to access the params.prometheus_pid_file property as # format(‘{prometheus_pid_file}’) env.set_params(params)
  • # Configure prometheussearch self.configure(env)
  • # Start prometheussearch cmd = format(“systemctl start prometheus.service”)
  • Execute(cmd, user=”root”)
  • Execute(
  • ‘ps -ef | grep prometheus | grep -v grep | awk \'{print$2}\’ > ‘ + params.prometheus_pid_file, user=params.prometheus_user)
  • # def check_process_status(pid_file): # if not pid_file or not os.path.isfile(pid_file): # logging.error(“Pid file {0} is empty or does not exist”.format(str(pid_file))) # return False # else: # pid = -1 # try: # pid = int(read_file(pid_file)) # except RuntimeError: # logging.error(“Pid file {0} does not exist or does not contain a process id number”.format(pid_file)) # try: # # Kill will not actually kill the process # # From the doc: # # If sig is 0, then no signal is sent, but error checking is still # # performed; this can be used to check for the existence of a # # process ID or process group ID. # os.kill(pid, 0) # logging.info(“process already exists! “) # process_is_exist = True # except OSError: # logging.error(“Process with pid {0} is not running. Stale pid file” # ” at {1}”.format(pid, pid_file)) # process_is_exist = False # return process_is_exist # def status(self, env): # # Import properties defined in -env.xml file from the status_params class # import status_params # # This allows us to access the params.prometheus_pid_file property as # # format(‘{prometheus_pid_file}’) # env.set_params(status_params) # # try: # # pid_file = glob.glob(status_params.prometheus_pid_file)[0] # # except IndexError: # # pid_file = ” # # Use built-in method to check status using pidfile # check_process_status(status_params.prometheus_pid_file) def check_prometheus_status(self, pid_file):
  • from datetime import datetime
  • from resource_management.core.exceptions import ComponentIsNotRunning
  • from resource_management.core import sudo
  • from subprocess import PIPE,Popen
  • import shlex, subprocess
  • if not pid_file or not os.path.isfile(pid_file):
  • raise ComponentIsNotRunning()
  • try:
  • pid = str(sudo.read_file(pid_file))
  • cmd_line = “/usr/bin/yarn application -list” args = shlex.split(cmd_line)
  • proc = Popen(args, stdout=PIPE)
  • p = str(proc.communicate()[0].split())
  • if p.find(pid.strip()) < 0:
  • raise ComponentIsNotRunning()
  • except Exception, e:
  • raise ComponentIsNotRunning()
  • def status(self, env):
  • import status_params
  • from datetime import datetime
  • self.check_prometheus_status(status_params.flink_pid_file)
  • if __name__ == “__main__”:
  • Master().execute()

作者 admin

张宴银,大数据开发工程师

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注