0


python开发prometheus exporter--用于hadoop-yarn监控

首先写python的exporter需要知道Prometheus提供4种类型Metrics

分别是:Counter, Gauge, Summary和Histogram

  • Counter可以增长,并且在程序重启的时候会被重设为0,常被用于任务个数,总处理时间,错误个数等只增不减的指标。

  • Gauge与Counter类似,唯一不同的是Gauge数值可以减少,常被用于温度、利用率等指标。

  • Summary/Histogram概念比较复杂,对于我来说目前没有使用场景,暂无了解。

我们需要的pip模块

  1. from prometheus_client import CollectorRegistry, Gauge, push_to_gateway, start_http_server
  2. -----
  3. pip install prometheus_client

代码思路实例

  1. def push_yarn():
  2. # 监控zk_RM
  3. Yarn_zkRMAppRoot()
  4. # 监控yarn任务信息
  5. Yarn_AppsInfo()
  6. def run():
  7. start_http_server(8006) # 8006端口启动
  8. while True:
  9. push_yarn()
  10. time.sleep(10)
  11. if __name__ == '__main__':
  12. run()

push_yarn()为监控的数据数据

循环进行监控拿取数据进行监控

我们使用Gauge实例

注意⚠️:Gauge与Counter类似,唯一不同的是Gauge数值可以减少,常被用于温度、利用率等指标。

新增Gauge实例

  1. yarn_zkRMAppRoot_code = Gauge('yarn_zkRMAppRoot', 'yarn_zkRMAppRoot_num', ['instance'])
  2. started_time_gauge = Gauge('yarn_started_time', 'started_time', ['application'])
  3. launch_time_gauge = Gauge('yarn_launch_time', 'launch_time', ['application'])
  4. finished_time_gauge = Gauge('yarn_finished_time', 'finished_time', ['application'])
  5. memory_seconds_gauge = Gauge('yarn_memory_seconds', 'memory_seconds', ['application'])
  6. vcore_seconds_gauge = Gauge('yarn_vcore_seconds', 'vcore_seconds', ['application'])

yarn_zkRMAppRoot_code: 这个是一个Gauge指标,用于记录YARN ResourceManager应用程序根目录在ZooKeeper中的znode数量。

yarn_started_time: 这是一个Gauge指标,用于记录应用程序的启动时间。这个指标有一个 application 标签,用于区分不同的应用程序。

yarn_launch_time: 这是一个Gauge指标,用于记录应用程序的启动时间。这个指标也有一个 application 标签。

yarn_finished_time: 这是一个Gauge指标,用于记录应用程序的结束时间。这个指标也有一个 application 标签。

yarn_memory_seconds: 这是一个Gauge指标,用于记录应用程序使用的内存数量乘以运行时间(内存-秒)。这个指标也有一个 application 标签。

yarn_vcore_seconds: 这是一个Gauge指标,用于记录应用程序使用的虚拟CPU核心数量乘以运行时间(vCore-秒)。这个指标也有一个 application 标签。

实现一下我们要监控的指标

  1. # --------yarn-------- #####
  2. def Yarn_zkRMAppRoot():
  3. # 命令
  4. # 命令
  5. if kerberos_switch:
  6. command = f'''
  7. echo 'ls /rmstore/ZKRMStateRoot/RMAppRoot' | /opt/dtstack/DTBase/zookeeper/bin/zkCli.sh | grep application_ | awk -F , '{{print NF}}'
  8. '''
  9. else:
  10. command = f'''
  11. export CLIENT_JVMFLAGS="$CLIENT_JVMFLAGS -Djava.security.auth.login.config=/opt/dtstack/DTBase/zookeeper/conf/jaas.conf -Djava.security.krb5.conf=/opt/dtstack/Kerberos/kerberos_pkg/conf/krb5.conf -Dzookeeper.server.principal=zookeeper/{hostname}@DTSTACK.COM"
  12. echo 'ls /rmstore/ZKRMStateRoot/RMAppRoot' | /opt/dtstack/DTBase/zookeeper/bin/zkCli.sh | grep application_ | awk -F , '{{print NF}}'
  13. '''
  14. # 使用subprocess模块执行命令
  15. result = subprocess.getstatusoutput(command) # (0, '455')
  16. if result[0] == 0:
  17. yarn_zkRMAppRoot_code.labels('yarn_' + hostname).set(result[1])
  18. else:
  19. print(f"Failed to execute command: {command}")
  20. def Yarn_AppsInfo():
  21. list_apps = []
  22. command = "yarn rmadmin -getServiceState rm1"
  23. apps_url = "http://{}/ws/v1/cluster/apps"
  24. rm_info = subprocess.getstatusoutput(command)
  25. if rm_info[0] == 0:
  26. if rm_info[1] == 'active':
  27. rm_host = yarn_rm1
  28. else:
  29. rm_host = yarn_rm2
  30. response = requests.get(url=apps_url.format(rm_host))
  31. html = response.text
  32. data = json.loads(html)
  33. for i in range(0, len(data['apps']['app'])):
  34. need_data = data['apps']['app']
  35. if need_data[i]['memorySeconds'] > 102400: # 大于10G的任务
  36. list_apps.append([need_data[i]['id'],
  37. need_data[i]['startedTime'],
  38. need_data[i]['launchTime'],
  39. need_data[i]['finishedTime'],
  40. need_data[i]['memorySeconds'], need_data[i]['vcoreSeconds']])
  41. sorted_lst = sorted(list_apps, key=lambda x: (x[4], x[5]))
  42. for list in sorted_lst:
  43. application = list[0]
  44. started_time = list[1]
  45. launch_time = list[2]
  46. finished_time = list[3]
  47. memory_seconds = list[4]
  48. vcore_seconds = list[5]
  49. started_time_gauge.labels(application=application).set(started_time)
  50. launch_time_gauge.labels(application=application).set(launch_time)
  51. finished_time_gauge.labels(application=application).set(finished_time)
  52. memory_seconds_gauge.labels(application=application).set(memory_seconds)
  53. vcore_seconds_gauge.labels(application=application).set(vcore_seconds)

其中Yarn_zkRMAppRoot是检测znode数量的

Yarn_AppsInfo是检测大于10G的任务的

传到服务器启动这个exporter

python3 mg_exporter.py

访问http://172.16.121.89:8006/metrics

然后加入prometheus配置中就可以检测到了


本文转载自: https://blog.csdn.net/cz124560/article/details/140291166
版权归原作者 超级迅猛龙 所有, 如有侵权,请联系我们删除。

“python开发prometheus exporter--用于hadoop-yarn监控”的评论:

还没有评论