- 论坛徽章:
- 27
|
我亲爱的朋友们,刚写完,就迫不及待的贴上来了,- -module(my_ping).
- -behaviour(gen_event).
- -compile(export_all).
- t_ping()->
- put(count,1),
- put(total,1),
- gen_event:start({local,monitor}),
- gen_event:add_handler(monitor,my_ping,"warning.txt"),
- P = open_port({spawn,"c:/windows/system32/PING.exe -t www.chinaunix.net"},[binary]),
- loop(P).
- loop(P)->
- receive
- {P,{data,Data}} ->
- case re:run(Data,<<"(\\d+)ms\s">>,[{capture,[1],list}]) of
- {match,[Res]} -> io:format("~n~p",[Res]),
- Num=erlang:list_to_integer(Res),
- average(Num),
- alert(Num);
- _ -> ok
- end,
- loop(P)
- end.
- alert(Num)->
- case Num > 10 of
- true -> io:format("!!!! warning"),gen_event:notify(monitor,[Num]);
- false -> ok
- end.
- average(Num)->
- C=get(count),
- T=get(total),
- P=T/C,
- io:format(" * average is: ~p~n",[P]),
- put(count,C+1),
- put(total,T+Num).
- %%% gen_event part
- init(File)->
- {ok,Fd} = file:open(File,write),
- {ok,Fd}.
- handle_event(Event,Fd)->
- io:format(Fd,"[Warning] ~p~n",[Event]),
- {ok,Fd}.
- terminate(_Args,Fd)->
- file:close(Fd).
复制代码 |
|