1#!/usr/bin/python 2# 3# Copyright (c) 2015 The Chromium OS Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7 8"""Script to check the number of long-running processes. 9 10This script gets the number of processes for "gsutil" and "autoserv" 11that are running more than 24 hours, and throws the number to stats 12dashboard. 13 14This script depends on the "etimes" user-defined format of "ps". 15Goobuntu 14.04 has the version of ps that supports etimes, but not 16Goobuntu 12.04. 17""" 18 19 20import socket 21import subprocess 22 23import common 24from autotest_lib.client.common_lib.cros.graphite import autotest_stats 25 26 27STATS_KEY = 'hung_processes.%s' % socket.gethostname().replace('.', '_') 28 29 30def check_proc(prog, max_elapsed_sec): 31 """Check the number of long-running processes for a given program. 32 33 Finds out the number of processes for a given program that have run 34 more than a given elapsed time. 35 Sends out the number to stats dashboard. 36 37 @param prog: Program name. 38 @param max_elapsed_sec: Max elapsed time in seconds. Processes that 39 have run more than this value will be caught. 40 """ 41 cmd = ('ps -eo etimes,args | grep "%s" | awk \'{if($1 > %d) print $0}\' | ' 42 'wc -l' % (prog, max_elapsed_sec)) 43 count = int(subprocess.check_output(cmd, shell = True)) 44 autotest_stats.Gauge(STATS_KEY).send(prog, count) 45 46 47def main(): 48 for p in ('gsutil', 'autoserv'): 49 check_proc(p, 86400) 50 51 52if __name__ == '__main__': 53 main() 54