Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
219f59f2
C
cat
项目概览
梦中观雨
/
cat
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
cat
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
219f59f2
编写于
3月 21, 2014
作者:
木子技术组
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update switchcat.py
上级
9f2423dc
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
68 addition
and
66 deletion
+68
-66
cat-script/switchcat.py
cat-script/switchcat.py
+68
-66
未找到文件。
cat-script/switchcat.py
浏览文件 @
219f59f2
...
@@ -5,34 +5,38 @@ import subprocess
...
@@ -5,34 +5,38 @@ import subprocess
import
time
import
time
import
urllib2
import
urllib2
monitor
=
[{
'
name'
:
'ctc
'
,
monitor
=
[{
'
group'
:
'switch
'
,
'type'
:
'cisco'
,
'type'
:
'cisco'
,
'public'
:
'dianpingP@ssword'
,
'public'
:
'dianpingP@ssword'
,
'version'
:
'2c'
,
'version'
:
'2c'
,
'ip'
:
'172.25.21.88'
,
'ip'
:
'172.25.21.88'
,
'group'
:[{
'port'
:[
10101
,
10102
,
10601
,
10602
]
'key'
:
'to-outer'
,
'port'
:[
10101
,
10102
,
10601
,
10602
],
'catch'
:
0b1111
}]
}]
}]
CISCO_IN_TRAFFIC_OID
=
'.1.3.6.1.2.1.2.2.1.10.'
CISCO_IN_TRAFFIC_OID
=
'.1.3.6.1.2.1.2.2.1.10.'
CISCO_IN_COUNT_OID
=
'.1.3.6.1.2.1.2.2.1.11.'
CISCO_IN_COUNT_OID
=
'.1.3.6.1.2.1.2.2.1.11.'
CISCO_OUT_TRAFFIC_OID
=
'.1.3.6.1.2.1.2.2.1.16.'
CISCO_OUT_TRAFFIC_OID
=
'.1.3.6.1.2.1.2.2.1.16.'
CISCO_OUT_COUNT_OID
=
'.1.3.6.1.2.1.2.2.1.17.'
CISCO_OUT_COUNT_OID
=
'.1.3.6.1.2.1.2.2.1.17.'
CISCO_NAME_OID
=
'.1.3.6.1.2.1.1.5'
CISCO_PORT_NAME_OID
=
'.1.3.6.1.2.1.2.2.1.2.'
SLEEP_TIME
=
8
#s
SLEEP_TIME
=
8
#s
SNMP_CMD
=
'snmpget'
SERVER
=
'10.128.120.38:2281'
SERVER
=
'10.128.120.38:2281'
DATA_GROUP
=
'TestGroup'
DATA_DOMAIN
=
'net'
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
while
True
:
while
True
:
try
:
try
:
for
unit
in
monitor
:
for
unit
in
monitor
:
cmdpre
=
SNMP_CMD
+
' -c '
+
unit
[
'public'
]
+
' -v '
+
unit
[
'version'
]
+
' '
+
unit
[
'ip'
]
+
' '
if
not
unit
.
get
(
'name'
):
try
:
cmd
=
'snmpwalk -c '
+
unit
[
'public'
]
+
' -v '
+
unit
[
'version'
]
+
' '
+
unit
[
'ip'
]
+
' '
+
CISCO_NAME_OID
p
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
shell
=
True
)
p
.
wait
()
unit
[
'name'
]
=
p
.
stdout
.
read
().
split
(
': '
)[
1
].
strip
()
except
Exception
,
e
:
print
str
(
e
)
unit
[
'name'
]
=
'unknown'
cmd
=
'snmpget -c '
+
unit
[
'public'
]
+
' -v '
+
unit
[
'version'
]
+
' '
+
unit
[
'ip'
]
+
' '
if
unit
[
'type'
]
==
'cisco'
:
if
unit
[
'type'
]
==
'cisco'
:
in_traffic_oid
=
CISCO_IN_TRAFFIC_OID
in_traffic_oid
=
CISCO_IN_TRAFFIC_OID
out_traffic_oid
=
CISCO_OUT_TRAFFIC_OID
out_traffic_oid
=
CISCO_OUT_TRAFFIC_OID
...
@@ -41,63 +45,61 @@ if __name__ == '__main__':
...
@@ -41,63 +45,61 @@ if __name__ == '__main__':
else
:
else
:
print
'The unit '
+
unit
[
'name'
]
+
'
\'
s type can not resolved!! ['
+
unit
[
'type'
]
+
']'
print
'The unit '
+
unit
[
'name'
]
+
'
\'
s type can not resolved!! ['
+
unit
[
'type'
]
+
']'
continue
continue
for
group
in
unit
[
'group'
]:
if
not
unit
.
get
(
'portname'
):
if
not
group
:
try
:
cmd
=
'snmpget -c '
+
unit
[
'public'
]
+
' -v '
+
unit
[
'version'
]
+
' '
+
unit
[
'ip'
]
+
' '
if
unit
[
'type'
]
==
'cisco'
:
for
p
in
unit
[
'port'
]:
cmd
+=
CISCO_PORT_NAME_OID
+
str
(
p
)
+
' '
else
:
raise
Exception
(
'unknown device type!'
)
p
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
shell
=
True
)
p
.
wait
()
unit
[
'portname'
]
=
p
.
stdout
.
read
().
split
(
': '
)[
1
].
strip
()
except
Exception
,
e
:
print
str
(
e
)
unit
[
'portname'
]
=
unit
[
'port'
]
for
port
in
unit
[
'port'
]:
if
not
port
:
continue
continue
cmd
=
cmdpre
cmd
+=
in_traffic_oid
+
str
(
port
)
+
' '
for
port
in
group
[
'port'
]:
cmd
+=
out_traffic_oid
+
str
(
port
)
+
' '
cmd
+=
in_traffic_oid
+
str
(
port
)
+
' '
cmd
+=
in_count_oid
+
str
(
port
)
+
' '
cmd
+=
out_traffic_oid
+
str
(
port
)
+
' '
cmd
+=
out_count_oid
+
str
(
port
)
+
' '
cmd
+=
in_count_oid
+
str
(
port
)
+
' '
p
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
shell
=
True
)
cmd
+=
out_count_oid
+
str
(
port
)
+
' '
p
.
wait
()
p
=
subprocess
.
Popen
(
cmd
,
stdout
=
subprocess
.
PIPE
,
shell
=
True
)
result
=
[
r
.
split
(
': '
)[
1
]
for
r
in
p
.
stdout
.
read
().
split
(
'
\n
'
)
if
r
]
p
.
wait
()
if
not
unit
.
get
(
'last'
):
if
not
group
.
get
(
'last'
):
unit
[
'last'
]
=
[
int
(
r
)
for
r
in
result
]
group
[
'last'
]
=
[
int
(
r
.
split
(
': '
)[
1
])
for
r
in
p
.
stdout
.
read
().
split
(
'
\n
'
)
if
r
]
else
:
else
:
now
=
[
int
(
r
)
for
r
in
result
]
now
=
[
int
(
r
.
split
(
': '
)[
1
])
for
r
in
p
.
stdout
.
read
().
split
(
'
\n
'
)
if
r
]
diff
=
[]
diff
=
[]
i
=
0
i
=
0
while
i
<
len
(
now
):
while
i
<
len
(
now
):
d
=
now
[
i
]
-
unit
[
'last'
][
i
]
d
=
now
[
i
]
-
group
[
'last'
][
i
]
if
d
<
0
:
if
d
<
0
:
d
+=
4294967296
d
+=
4294967296
diff
.
append
(
d
)
diff
.
append
(
d
)
i
+=
1
i
+=
1
unit
[
'last'
]
=
now
group
[
'last'
]
=
now
print
unit
[
'last'
]
if
group
[
'catch'
]
&
0b1000
:
i
=
0
data
=
sum
(
diff
[::
4
])
while
i
<
len
(
unit
[
'port'
]):
print
'in-traffic:'
+
str
(
data
)
try
:
try
:
#in
urllib2
.
urlopen
(
'http://'
+
SERVER
+
'/cat/r/systemMonitor?group='
+
DATA_GROUP
+
'&domain='
+
print
'http://'
+
SERVER
+
'/cat/r/systemMonitor?group='
+
unit
[
'group'
]
+
'&domain='
+
\
DATA_DOMAIN
+
'&key='
+
unit
[
'name'
]
+
'-'
+
group
[
'key'
]
+
'/in-traffic'
+
'&op=sum&sum='
+
str
(
data
),
timeout
=
0
)
unit
[
'name'
]
+
'&key='
+
str
(
unit
[
'portname'
][
i
])
+
'-in&op=sum&sum='
+
str
(
diff
[::
4
][
i
])
except
Exception
,
e
:
#urllib2.urlopen('http://'+SERVER+'/cat/r/systemMonitor?group='+unit['group']+'&domain='+
pass
# unit['name']+'&key='+unit['name']+'-'+group['key']+'/in-traffic'+'&op=sum&sum='+str(data),timeout=0)
if
group
[
'catch'
]
&
0b0100
:
#out
data
=
sum
(
diff
[
1
::
4
])
print
'http://'
+
SERVER
+
'/cat/r/systemMonitor?group='
+
unit
[
'group'
]
+
'&domain='
+
\
print
'out-traffic:'
+
str
(
data
)
unit
[
'name'
]
+
'&key='
+
str
(
unit
[
'portname'
][
i
])
+
'-in&op=sum&sum='
+
str
(
diff
[
1
::
4
][
i
])
try
:
#urllib2.urlopen('http://'+SERVER+'/cat/r/systemMonitor?group='+unit['group']+'&domain='+
response
=
urllib2
.
urlopen
(
'http://'
+
SERVER
+
'/cat/r/systemMonitor?group='
+
DATA_GROUP
+
'&domain='
+
# unit['name']+'&key='+unit['name']+'-'+group['key']+'/in-traffic'+'&op=sum&sum='+str(data),timeout=0)
DATA_DOMAIN
+
'&key='
+
unit
[
'name'
]
+
'-'
+
group
[
'key'
]
+
'/out-traffic'
+
'&op=sum&sum='
+
str
(
data
),
timeout
=
0
)
except
Exception
,
e
:
except
Exception
,
e
:
print
str
(
e
)
pass
i
+=
1
if
group
[
'catch'
]
&
0b0010
:
data
=
sum
(
diff
[
2
::
4
])
print
'in-packets:'
+
str
(
data
)
try
:
response
=
urllib2
.
urlopen
(
'http://'
+
SERVER
+
'/cat/r/systemMonitor?group='
+
DATA_GROUP
+
'&domain='
+
DATA_DOMAIN
+
'&key='
+
unit
[
'name'
]
+
'-'
+
group
[
'key'
]
+
'/in-packets'
+
'&op=sum&sum='
+
str
(
data
),
timeout
=
0
)
except
Exception
,
e
:
pass
if
group
[
'catch'
]
&
0b0001
:
data
=
sum
(
diff
[
3
::
4
])
print
'out-packets:'
+
str
(
data
)
try
:
response
=
urllib2
.
urlopen
(
'http://'
+
SERVER
+
'/cat/r/systemMonitor?group='
+
DATA_GROUP
+
'&domain='
+
DATA_DOMAIN
+
'&key='
+
unit
[
'name'
]
+
'-'
+
group
[
'key'
]
+
'/out-packets'
+
'&op=sum&sum='
+
str
(
data
),
timeout
=
0
)
except
Exception
,
e
:
pass
except
Exception
,
e
:
except
Exception
,
e
:
print
'exception!!!
'
print
'exception!!!
'
+
str
(
e
)
time
.
sleep
(
SLEEP_TIME
)
time
.
sleep
(
SLEEP_TIME
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录