import
MySQLdb
import
sys
import
os
def
get_total_value(sql):
db
=
MySQLdb.connect(host
=
'xxx'
,user
=
'xxxx'
,passwd
=
'xxx'
,db
=
'xxx'
)
cursor
=
db.cursor()
cursor.execute(sql)
try
:
result
=
cursor.fetchone()[
0
]
except
:
result
=
0
cursor.close()
db.close()
return
result
if
__name__
=
=
'__main__'
:
sql
=
''
if
sys.argv[
1
]
=
=
"all_mapTaskSlots"
:
sql
=
"select sum(lastvalue) from hosts a, items b where key_ = 'hadoop_metrics[mrmetrics.log,mapred.tasktracker,mapTaskSlots]' and lower(host) like '%-hadoop-datanode%' and a.hostid = b.hostid"
elif
sys.argv[
1
]
=
=
"all_maps_running"
:
sql
=
"select sum(lastvalue) from hosts a, items b where key_ = 'hadoop_metrics[mrmetrics.log,mapred.tasktracker,maps_running]' and lower(host) like '%-hadoop-datanode%' and a.hostid = b.hostid"
elif
sys.argv[
1
]
=
=
"all_reduceTaskSlots"
:
sql
=
"select sum(lastvalue) from hosts a, items b where key_ = 'hadoop_metrics[mrmetrics.log,mapred.tasktracker,reduceTaskSlots]' and lower(host) like '%-hadoop-datanode%' and a.hostid = b.hostid"
elif
sys.argv[
1
]
=
=
"all_reduces_running"
:
sql
=
"select sum(lastvalue) from hosts a, items b where key_ = 'hadoop_metrics[mrmetrics.log,mapred.tasktracker,reduces_running]' and lower(host) like '%-hadoop-datanode%' and a.hostid = b.hostid"
elif
sys.argv[
1
]
=
=
"all_ThreadsBlocked"
:
sql
=
"select sum(lastvalue) from hosts a, items b where key_ = 'hadoop_stats[datanode,ThreadsBlocked]' and lower(host) like '%-hadoop-datanode%' and a.hostid = b.hostid"
elif
sys.argv[
1
]
=
=
"all_ThreadsRunnable"
:
sql
=
"select sum(lastvalue) from hosts a, items b where key_ = 'hadoop_stats[datanode,ThreadsRunnable]' and lower(host) like '%-hadoop-datanode%' and a.hostid = b.hostid"
elif
sys.argv[
1
]
=
=
"all_ThreadsWaiting"
:
sql
=
"select sum(lastvalue) from hosts a, items b where key_ = 'hadoop_stats[datanode,ThreadsWaiting]' and lower(host) like '%-hadoop-datanode%' and a.hostid = b.hostid"
else
:
sys.exit(
0
)
value
=
get_total_value(sql)
print
value