1
%% ``The contents of this file are subject to the Erlang Public License,
4
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
2
7
%% Version 1.1, (the "License"); you may not use this file except in
3
8
%% compliance with the License. You should have received a copy of the
4
9
%% Erlang Public License along with this software. If not, it can be
5
%% retrieved via the world wide web at http://www.erlang.org/.
10
%% retrieved online at http://www.erlang.org/.
7
12
%% Software distributed under the License is distributed on an "AS IS"
8
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
14
%% the License for the specific language governing rights and limitations
10
15
%% under the License.
12
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
%% AB. All Rights Reserved.''
249
250
{ok, [N], _} = io_lib:fread("~d", Ps),
253
get_uint32_measurement(Request, #internal{os_type = {unix, dragonfly}}) ->
254
D = os:cmd("/sbin/sysctl -n vm.loadavg") -- "\n",
255
{ok,[Load1,Load5,Load15],_} = io_lib:fread("{ ~f ~f ~f }", D),
256
%% We could count the lines from the ps command as well
258
?avg1 -> sunify(Load1);
259
?avg5 -> sunify(Load5);
260
?avg15 -> sunify(Load15);
263
Ps = os:cmd("/bin/ps -ax | /usr/bin/wc -l"),
264
{ok, [N], _} = io_lib:fread("~d", Ps),
252
267
get_uint32_measurement(Request, #internal{os_type = {unix, openbsd}}) ->
253
268
D = os:cmd("/sbin/sysctl -n vm.loadavg") -- "\n",
254
269
{ok, [L1, L5, L15], _} = io_lib:fread("~f ~f ~f", D),
620
636
{Pid, ?nprocs} ->
621
637
port_command(Port, ?nprocs),
622
638
Result = port_receive_uint32(Port, Timeout),
623
port_server_handle_reply(Port, Pid, Result, Timeout);
639
Pid ! {self(), {data, Result}},
640
port_server_loop(Port, Timeout);
625
642
% Average load for the past minute
627
644
port_command(Port, ?avg1),
628
645
Result = port_receive_uint32(Port, Timeout),
629
port_server_handle_reply(Port, Pid, Result, Timeout);
646
Pid ! {self(), {data, Result}},
647
port_server_loop(Port, Timeout);
631
649
% Average load for the past five minutes
633
651
port_command(Port, ?avg5),
634
652
Result = port_receive_uint32(Port, Timeout),
635
port_server_handle_reply(Port, Pid, Result, Timeout);
653
Pid ! {self(), {data, Result}},
654
port_server_loop(Port, Timeout);
637
656
% Average load for the past 15 minutes
639
658
port_command(Port, ?avg15),
640
659
Result = port_receive_uint32(Port, Timeout),
641
port_server_handle_reply(Port, Pid, Result, Timeout);
660
Pid ! {self(), {data, Result}},
661
port_server_loop(Port, Timeout);
644
664
port_command(Port, ?util),
645
665
Result = port_receive_util(Port, Timeout),
646
port_server_handle_reply(Port, Pid, Result, Timeout);
666
Pid ! {self(), {data, Result}},
667
port_server_loop(Port, Timeout);
650
671
port_command(Port, ?ping),
651
672
Result = port_receive_uint32(Port, Timeout),
652
port_server_handle_reply(Port, Pid, Result, Timeout);
673
Pid ! {self(), {data, Result}},
674
port_server_loop(Port, Timeout);
654
676
% Close port and this server
661
683
% Ignore other commands
662
_Other -> port_server_loop(Port, Timeout)
684
_ -> port_server_loop(Port, Timeout)
665
port_server_handle_reply(Port, Pid, Reply, Timeout) ->
668
%%% FIXME: Could this happen?
670
% Explain error for the client
671
Pid ! {self(), {error, Reason}},
672
% Kill and clean the port and its messages
673
port_command(Port, ?quit),
675
port_server_flush_messages(Port),
679
% All is fine, send result to client
680
Pid ! {self(), {data, Result}},
681
port_server_loop(Port, Timeout)
685
port_server_flush_messages(Port) ->
686
receive {'EXIT', Port, _} -> ok after 0 -> ok end,
687
receive {Port, {data, _}} -> ok after 0 -> ok end.
689
687
port_receive_uint32( Port, Timeout) -> port_receive_uint32(Port, Timeout, []).
690
688
port_receive_uint32(_Port, _Timeout, [D3,D2,D1,D0]) -> ?INT32(D3,D2,D1,D0);
691
689
port_receive_uint32(_Port, _Timeout, [_,_,_,_ | G]) -> exit({port_garbage, G});