import
os
import
subprocess
import
util
import
re
import
sendmail
import
time
import
sys
import
property
if
__name__
=
=
"__main__"
:
allInfo
=
[]
now
=
time.time()
sql
=
"show locks"
lock_query_id
=
""
lock_create_time
=
""
lock_sql
=
""
allLock
=
util.hive_run_cmd(sql)
for
line
in
allLock:
if
len
(re.split(
'\t| '
,line)[
0
].split(
'@'
))
=
=
2
:
dataBase
=
re.split(
'\t| '
,line)[
0
].split(
'@'
)[
0
]
dataTable
=
re.split(
'\t| '
,line)[
0
].split(
'@'
)[
1
]
lockType
=
re.split(
'\t| '
,line)[
-
1
].strip()
print
dataBase
+
"==="
+
dataTable
+
"==="
+
lockType
util.get_lock_info(allInfo,database
=
dataBase,table
=
dataTable,keytype
=
lockType)
else
:
dataBase
=
re.split(
'\t| '
,line)[
0
].split(
'@'
)[
0
]
dataTable
=
re.split(
'\t| '
,line)[
0
].split(
'@'
)[
1
]
dataPartition
=
re.split(
'\t| '
,line)[
0
].split(
'@'
)[
2
].replace(
'/'
,
','
)
lockType
=
re.split(
'\t| '
,line)[
-
1
].strip()
print
dataBase
+
"==="
+
dataTable
+
"==="
+
lockType
+
"===="
+
dataPartition
util.get_lock_info(allInfo,database
=
dataBase,table
=
dataTable,keytype
=
lockType,partition
=
dataPartition)
print
allInfo
if
len
(allInfo)
=
=
0
:
pass
else
:
mailfile
=
open
(
"/home/hdfs/ericni/lock_monitor/mail/lock_table_"
+
str
(now)
+
".html"
,
"w+"
)
mailcontent
=
for
line
in
allInfo:
if
len
(line) <
5
:
pass
else
:
re_table
=
line[
0
]
re_type
=
line[
1
]
re_time
=
float
(now)
-
float
(line[
3
])
print
re_time
re_query
=
line[
2
]
re_sql
=
line[
4
]
if
(re_time >
=
1800
and
str
(re_type)
=
=
"SHARED"
)
or
(re_time >
=
600
and
str
(re_type)
=
=
"EXCLUSIVE"
):
print
"++++++++++++++++++++++++++++++++++++++++++++++"
cmd
=
"/bin/touch /tmp/alert.file"
os.popen(cmd)
mailcontent
+
=
%
(re_table)
mailcontent
+
=
%
(re_type)
mailcontent
+
=
%
(
round
(
float
(re_time),
2
))
mailcontent
+
=
%
(re_query)
mailcontent
+
=
%
(re_sql)
mailcontent
+
=
"</tr>"
else
:
pass
mailcontent
+
=
"</table>"
mailfile.write(mailcontent)
if
not
os.path.isfile(
"/tmp/alert.file"
):
print
"no need to alert"
sys.exit(
0
)
else
:
print
"+++______++++"
cmd
=
"/bin/rm -f /tmp/alert.file"
os.popen(cmd)
sendmail.send_mail_withoutSSL(
"HIVE table lock alert"
,mailcontent.encode(
'utf-8'
),
property
.mail_list_hdfs)