[api] add support for logging to the Drupal watchdog table

This commit is contained in:
Bart Van Der Meerssche 2011-03-29 07:45:59 +00:00
parent 1cba075938
commit 304dbbb803
3 changed files with 30 additions and 6 deletions

View File

@ -30,6 +30,7 @@ ensure_started(App) ->
end.
mysql_prepare() ->
mysql:prepare(watchdog, <<"INSERT INTO watchdog (uid, type, message, variables, severity, location, hostname, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?)">>),
mysql:prepare(permissions, <<"SELECT permissions FROM logger_tokens WHERE meter = ? AND token = ?">>),
mysql:prepare(device_key, <<"SELECT sha FROM (logger_devices ld INNER JOIN logger_meters lm ON ld.device = lm.device) WHERE lm.meter = ?">>),
mysql:prepare(sensor_props, <<"SELECT uid, device, night FROM logger_meters WHERE meter = ?">>),

View File

@ -165,3 +165,26 @@ time_to_seconds(Time) ->
false -> false;
{_Time, TimeSec} -> TimeSec
end.
% severity levels
-define(EMERGENCY, 0).
-define(ALERT, 1).
-define(CRITICAL, 2).
-define(ERROR, 3).
-define(WARNING, 4).
-define(NOTICE, 5).
-define(INFO, 6).
-define(DEBUG, 7).
% log to Drupal's watchdog table
logger(Uid, Type, Message, Severity, ReqData) ->
mysql:execute(pool, watchdog,
[Uid,
Type,
Message,
<<"a:0:{}">>,
Severity,
list_to_binary(wrq:raw_path(ReqData)),
list_to_binary(wrq:peer(ReqData)),
unix_time()
]).

View File

@ -153,17 +153,17 @@ process_post(ReqData, #state{rrdSensor = RrdSensor} = State) ->
case erlrrd:update([?BASE_PATH, [RrdSensor|".rrd"], " ", RrdData]) of
{ok, _RrdResponse} ->
RrdResponse = "ok",
NewMidnight = update_night(RrdSensor, Uid, Midnight, LastTimestamp),
NewMidnight = update_night(RrdSensor, Uid, Midnight, LastTimestamp, ReqData),
mysql:execute(pool, sensor_update, [unix_time(), NewMidnight, LastValue, RrdSensor]);
{error, RrdResponse} ->
true
logger(Uid, <<"rrdupdate.base">>, list_to_binary(RrdResponse), ?ERROR, ReqData)
end,
JsonResponse = mochijson2:encode({struct, [{<<"response">>, list_to_binary(RrdResponse)}]}),
{true , wrq:set_resp_body(JsonResponse, ReqData), State}.
update_night(RrdSensor, Uid, Midnight, LastTimestamp) when LastTimestamp > Midnight + 6 * ?HOUR ->
update_night(RrdSensor, Uid, Midnight, LastTimestamp, ReqData) when LastTimestamp > Midnight + 6 * ?HOUR ->
LastMidnight = calculate_midnight(unix_time(), Uid),
case erlrrd:fetch(erlrrd:c([[?BASE_PATH, [RrdSensor|".rrd"]], "AVERAGE", ["-s ", integer_to_list(LastMidnight + 2 * ?HOUR)], ["-e ", integer_to_list(LastMidnight + 5 * ?HOUR)], ["-r ", integer_to_list(?QUARTER)]])) of
@ -175,12 +175,12 @@ update_night(RrdSensor, Uid, Midnight, LastTimestamp) when LastTimestamp > Midni
%debugging: io:format("~s~n", [[?NIGHT_PATH, [RrdSensor|".rrd"], " ", integer_to_list(LastMidnight + 5 * ?HOUR), ":", float_to_list(NightAverage)]]),
erlrrd:update([?NIGHT_PATH, [RrdSensor|".rrd"], " ", integer_to_list(LastMidnight + 5 * ?HOUR), ":", float_to_list(NightAverage)]);
{error, _Reason} ->
true
{error, Reason} ->
logger(Uid, <<"rrdupdate.night">>, list_to_binary(Reason), ?ERROR, ReqData)
end,
LastMidnight + ?DAY;
update_night(_RrdSensor, _Uid, Midnight, _LastTimestamp) ->
update_night(_RrdSensor, _Uid, Midnight, _LastTimestamp, _ReqData) ->
Midnight.
calculate_midnight(Timestamp, Uid) ->