Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦中观雨
cat
提交
08929de8
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,发现更多精彩内容 >>
提交
08929de8
编写于
3月 21, 2014
作者:
H
he.huang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
puppet script
上级
2aed2cf2
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
378 addition
and
0 deletion
+378
-0
cat-script/puppetMonitor/Alertation.java
cat-script/puppetMonitor/Alertation.java
+104
-0
cat-script/puppetMonitor/HttpPostUtils.java
cat-script/puppetMonitor/HttpPostUtils.java
+76
-0
cat-script/puppetMonitor/PuppetMonitor.java
cat-script/puppetMonitor/PuppetMonitor.java
+198
-0
未找到文件。
cat-script/puppetMonitor/Alertation.java
0 → 100644
浏览文件 @
08929de8
package
puppetMonitor
;
public
class
Alertation
{
private
String
type
;
private
String
ip
;
private
String
hostname
;
private
String
user
;
private
String
url
;
private
String
op
;
private
String
date
;
private
String
title
;
private
String
content
;
private
String
domain
;
public
String
getType
()
{
return
type
;
}
public
void
setType
(
String
type
)
{
this
.
type
=
type
;
}
public
String
getIp
()
{
return
ip
;
}
public
void
setIp
(
String
ip
)
{
this
.
ip
=
ip
;
}
public
String
getUser
()
{
return
user
;
}
public
void
setUser
(
String
user
)
{
this
.
user
=
user
;
}
public
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
public
String
getOp
()
{
return
op
;
}
public
void
setOp
(
String
op
)
{
this
.
op
=
op
;
}
public
String
getDate
()
{
return
date
;
}
public
void
setDate
(
String
date
)
{
this
.
date
=
date
;
}
public
String
getTitle
()
{
return
title
;
}
public
void
setTitle
(
String
title
)
{
this
.
title
=
title
;
}
public
String
getContent
()
{
return
content
;
}
public
void
setContent
(
String
content
)
{
this
.
content
=
content
;
}
public
String
getDomain
()
{
return
domain
;
}
public
void
setDomain
(
String
domain
)
{
this
.
domain
=
domain
;
}
public
String
getHostname
()
{
return
hostname
;
}
public
void
setHostname
(
String
hostname
)
{
this
.
hostname
=
hostname
;
}
}
cat-script/puppetMonitor/HttpPostUtils.java
0 → 100644
浏览文件 @
08929de8
package
puppetMonitor
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
public
class
HttpPostUtils
{
public
String
getUrlAddress
()
{
return
urlAddress
;
}
public
void
setUrlAddress
(
String
urlAddress
)
{
this
.
urlAddress
=
urlAddress
;
}
private
String
urlAddress
;
public
String
httpPost
(
String
[]
params
){
String
flag
=
"error"
;
URL
url
=
null
;
HttpURLConnection
con
=
null
;
BufferedReader
in
=
null
;
StringBuffer
result
=
new
StringBuffer
();
try
{
url
=
new
URL
(
this
.
urlAddress
);
con
=
(
HttpURLConnection
)
url
.
openConnection
();
con
.
setUseCaches
(
false
);
con
.
setDoOutput
(
true
);
con
.
setRequestMethod
(
"POST"
);
String
paramsTemp
=
""
;
for
(
String
param:
params
){
if
(
param
!=
null
&&!
""
.
equals
(
param
.
trim
())){
paramsTemp
+=
"&"
+
param
;
}
}
//test===============
// System.out.println(paramsTemp);
//test===============
byte
[]
b
=
paramsTemp
.
getBytes
();
con
.
getOutputStream
().
write
(
b
,
0
,
b
.
length
);
con
.
getOutputStream
().
flush
();
con
.
getOutputStream
().
close
();
in
=
new
BufferedReader
(
new
InputStreamReader
(
con
.
getInputStream
()));
while
(
true
)
{
String
line
=
in
.
readLine
();
if
(
line
==
null
)
{
break
;
}
else
{
result
.
append
(
line
);
}
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
try
{
if
(
in
!=
null
){
in
.
close
();
}
if
(
con
!=
null
){
con
.
disconnect
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
String
rs
=
result
.
toString
();
if
(
rs
.
contains
(
"200"
)){
flag
=
"ok"
;
}
return
flag
;
}
}
cat-script/puppetMonitor/PuppetMonitor.java
0 → 100644
浏览文件 @
08929de8
package
puppetMonitor
;
import
java.io.BufferedReader
;
import
java.io.BufferedWriter
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.FileReader
;
import
java.io.FileWriter
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.RandomAccessFile
;
import
java.text.DateFormat
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Locale
;
import
java.util.regex.*
;
import
java.util.Calendar
;
import
java.net.*
;
public
class
PuppetMonitor
{
private
static
String
m_logFile
;
private
static
String
m_lineFile
;
private
static
void
run
()
{
boolean
active
=
true
;
Long
end_position
=
0L
;
while
(
active
)
{
Long
position
=
getReaderPostion
(
m_lineFile
);
try
{
RandomAccessFile
reader
=
new
RandomAccessFile
(
m_logFile
,
"r"
);
reader
.
seek
(
position
);
String
line
=
null
;
while
((
line
=
reader
.
readLine
())!=
null
){
Alertation
alertation
=
parse
(
line
);
if
(
alertation
.
getDomain
()
!=
null
){
sendHttp
(
alertation
);
// break;
}
}
end_position
=
reader
.
getFilePointer
();
}
catch
(
IOException
e
){
System
.
out
.
println
(
"读文件异常:"
+
m_logFile
);
e
.
printStackTrace
();
}
finally
{
setReaderPostion
(
m_lineFile
,
end_position
);
}
try
{
Thread
.
sleep
(
1000
*
5
);
}
catch
(
InterruptedException
e
)
{
break
;
}
}
}
private
static
void
sendHttp
(
Alertation
alertation
)
{
String
[]
pars
=
new
String
[
10
];
pars
[
0
]=
"type="
+
alertation
.
getType
();
pars
[
1
]=
"title="
+
alertation
.
getTitle
();
pars
[
2
]=
"domain="
+
alertation
.
getDomain
();
pars
[
3
]=
"ip="
+
alertation
.
getIp
();
pars
[
4
]=
"user="
+
alertation
.
getUser
();
pars
[
5
]=
"content="
+
alertation
.
getContent
();
pars
[
6
]=
"url="
+
alertation
.
getUrl
();
pars
[
7
]=
"op="
+
alertation
.
getOp
();
pars
[
8
]=
"date="
+
alertation
.
getDate
();
pars
[
9
]=
"hostname="
+
alertation
.
getHostname
();
String
url
=
"http://10.128.120.12:2281/cat/r/alteration"
;
HttpPostUtils
httppost
=
new
HttpPostUtils
();
httppost
.
setUrlAddress
(
url
);
System
.
out
.
println
(
httppost
.
httpPost
(
pars
));
// System.out.println(alertation.getDate()+";"+alertation.getDomain()+";"+alertation.getHostname()+";"+alertation.getIp()+";"+alertation.getTitle()+";"+alertation.getContent());
}
private
static
Alertation
parse
(
String
line
)
{
String
type
=
"puppet"
;
String
user
=
"puppet"
;
String
url
=
""
;
String
op
=
"insert"
;
String
host
=
""
;
String
IP
=
""
;
String
date
=
""
;
String
domain
=
""
;
String
content
=
""
;
String
title
=
""
;
String
regEx
=
".*puppet-agent.*\\(\\/Stage"
;
Alertation
alertation
=
new
Alertation
();
if
(
Pattern
.
compile
(
regEx
).
matcher
(
line
).
find
())
{
String
[]
tmp_list
=
line
.
split
(
" "
);
Calendar
c
=
Calendar
.
getInstance
();
int
year
=
c
.
get
(
Calendar
.
YEAR
);
date
=
tmp_list
[
0
]
+
tmp_list
[
1
]+
" "
+
tmp_list
[
2
]
+
" "
+
tmp_list
[
3
]+
" "
+
Integer
.
toString
(
year
);
String
all_content
=
line
.
split
(
"\\("
)[
1
];
title
=
all_content
.
split
(
"\\)"
)[
0
].
split
(
"\\[main\\]\\/"
)[
1
];
content
=
all_content
.
split
(
"\\)"
)[
1
];
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
"MMMM dd HH:mm:ss yyyy"
,
Locale
.
US
);
SimpleDateFormat
sdf2
=
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
,
Locale
.
US
);
try
{
date
=
sdf2
.
format
(
sdf
.
parse
(
date
));
}
catch
(
ParseException
e1
)
{
e1
.
printStackTrace
();
}
// System.out.println(date);
try
{
InetAddress
ia
=
InetAddress
.
getLocalHost
();
host
=
ia
.
getHostName
();
IP
=
ia
.
getHostAddress
();
domain
=
host
.
split
(
"-sl-|-gp-|-ppe"
)[
0
];
}
catch
(
UnknownHostException
e
){
e
.
printStackTrace
();
}
alertation
.
setDate
(
date
);
alertation
.
setHostname
(
host
);
alertation
.
setIp
(
IP
);
alertation
.
setDomain
(
domain
);
alertation
.
setTitle
(
title
);
alertation
.
setContent
(
content
);
alertation
.
setOp
(
op
);
alertation
.
setUrl
(
url
);
alertation
.
setUser
(
user
);
alertation
.
setType
(
type
);
}
return
alertation
;
}
// private String getNextLine(int position) {
// return null;
// }
/**
*
* @param line_file,记录文件读取位置的文件
* @return 记录的数据,否则返回0
* 读取文件失败的时候是否创建文件line_file
*
*/
private
static
long
getReaderPostion
(
String
line_file
)
{
BufferedReader
reader
=
null
;
try
{
reader
=
new
BufferedReader
(
new
FileReader
(
line_file
));
String
str
=
reader
.
readLine
();
if
(
str
!=
null
)
{
return
Long
.
parseLong
(
str
);
}
}
catch
(
FileNotFoundException
e1
){
File
filename
=
new
File
(
line_file
);
try
{
filename
.
createNewFile
();
}
catch
(
IOException
e2
)
{
System
.
out
.
println
(
"创建文件失败:"
+
line_file
);
e2
.
printStackTrace
();
}
}
catch
(
Exception
e3
)
{
e3
.
printStackTrace
();
}
finally
{
if
(
reader
!=
null
)
{
try
{
reader
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
return
0L
;
}
private
static
void
setReaderPostion
(
String
line_file
,
Long
end_position
)
{
try
{
BufferedWriter
output
=
new
BufferedWriter
(
new
FileWriter
(
line_file
));
output
.
write
(
Long
.
toString
(
end_position
));
output
.
close
();
}
catch
(
IOException
e
){
System
.
out
.
println
(
"写入文件异常:line_file"
);
e
.
printStackTrace
();
}
}
public
static
void
main
(
String
args
[])
{
// if (args.length > 2) {
// m_logFile = args[0];
// m_lineFile = args[1];
// } else {
// System.out.println("Please check pars!");
// System.exit(0);
// }
m_logFile
=
"/Users/River/messages"
;
m_lineFile
=
"/var/log/line_random.log"
;
run
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录