Skip to content

Commit

Permalink
Merge pull request #7 from jaredbischof/master
Browse files Browse the repository at this point in the history
Adding mlog.py and editing some other files.
  • Loading branch information
jaredbischof committed Mar 13, 2013
2 parents 4c15bf2 + 0b8bfcb commit b99d26b
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 28 deletions.
2 changes: 1 addition & 1 deletion MCP
Expand Up @@ -10,7 +10,7 @@ from ConfigParser import SafeConfigParser
# Getting paths
MCP_path = os.path.realpath(__file__)
matchObj = re.match(r'(^.*\/)', MCP_path)
MCP_dir = matchObj.group()
MCP_dir = matchObj.group(0)

# Adding modules directory to path
path = list(sys.path)
Expand Down
57 changes: 30 additions & 27 deletions mlog_libs/mlog.pm
Expand Up @@ -15,7 +15,7 @@ use Sys::Syslog qw( :DEFAULT setlogsock);

require Exporter;

my $mlog_conf_file = "/etc/mlog/mlog.conf";
my $MLOG_CONF_FILE = "/etc/mlog/mlog.conf";
my $DEFAULT_LOG_LEVEL = 6;
my $MSG_CHECK_COUNT = 100;
my $MSG_CHECK_INTERVAL = 300; # 300s = 5min
Expand All @@ -38,7 +38,7 @@ mlog
=head1 DESCRIPTION
A wrapper for sending MGRAST logging to syslog.
A library for sending MG-RAST logging to syslog.
=head1 METHODS
Expand All @@ -48,7 +48,7 @@ logit(level, component, message, error_code): sends mgrast log message to syslog
=over 10
=item * level: (0-6) The logging level for this message is compared to the logging level that has been set in MGRAST_syslog. If it is <= the set logging level, the message will be sent to syslog, otherwise it will be ignored. Logging level is set to 6 if MGRAST control API cannot be reached.
=item * level: (0-6) The logging level for this message is compared to the logging level that has been set in mlog. If it is <= the set logging level, the message will be sent to syslog, otherwise it will be ignored. Logging level is set to 6 if MG-RAST control API cannot be reached.
=item * component: (string) This is the utility within MG-RAST that is logging the message. This is a free text field.
Expand Down Expand Up @@ -90,33 +90,16 @@ use_api_log_level(string component) : Removes the user-defined log level for thi
=cut

sub _get_log_level {
my ($component) = @_;
if(exists $user_defined_log_levels{$component}) {
return $user_defined_log_levels{$component};
} elsif(exists $api_defined_log_levels{$component}) {
return $api_defined_log_levels{$component};
} else {
return $DEFAULT_LOG_LEVEL;
}
}

sub _get_time_since_start {
my $now = DateTime->now( time_zone => 'local' )->set_time_zone('floating');
my $seconds_duration = $now->subtract_datetime_absolute($last_update_time);
return $seconds_duration->seconds;
}

sub init_mlog {
$last_update_msg_count = 0;
$last_update_time = DateTime->now( time_zone => 'local' )->set_time_zone('floating');

# Retrieving the control API defined log levels...
my $api_mlog_url = "";
open IN, "$mlog_conf_file" || print STDERR "Cannot open $mlog_conf_file for reading mlog configuration.\n";
open IN, "$MLOG_CONF_FILE" || print STDERR "Cannot open $MLOG_CONF_FILE for reading mlog configuration.\n";
while(my $line=<IN>) {
chomp $line;
if($line =~ /^url.*$/) {
if($line =~ /^url\s+.*$/) {
my @array = split(/\s+/, $line);
$api_mlog_url = $array[1];
}
Expand All @@ -136,6 +119,23 @@ sub init_mlog {
return 1;
}

sub _get_log_level {
my ($component) = @_;
if(exists $user_defined_log_levels{$component}) {
return $user_defined_log_levels{$component};
} elsif(exists $api_defined_log_levels{$component}) {
return $api_defined_log_levels{$component};
} else {
return $DEFAULT_LOG_LEVEL;
}
}

sub _get_time_since_start {
my $now = DateTime->now( time_zone => 'local' )->set_time_zone('floating');
my $seconds_duration = $now->subtract_datetime_absolute($last_update_time);
return $seconds_duration->seconds;
}

sub set_log_level {
my ($level, $component) = @_;
if($level !~ /^\d+$/ || $component eq "") {
Expand Down Expand Up @@ -186,16 +186,19 @@ sub logit {
return 0;
}

if($msg_count == 0 && $last_update_time eq "") {
print STDERR "WARNING: mlog_init() was not called, so I will call it for you.\n";
mlog_init();
}

unless($level =~ /^\d+$/ && $level >= 0 && $level <= 6) {
print STDERR "ERROR: mlog level '$level' is invalid, you must enter an integer between 0 and 6, inclusive.\n";
return 0;
}

++$msg_count;
++$last_update_msg_count;

if($msg_count == 0 && $last_update_time eq "") {
print STDERR "WARNING: mlog_init() was not called, so I will call it for you.\n";
mlog_init();
}

# May want to include these in 1st openlog argument
my $user = $ENV{'USER'};
my $ident = abs_path($0);
Expand Down
103 changes: 103 additions & 0 deletions mlog_libs/mlog.py
@@ -0,0 +1,103 @@
"""
NAME
mlog
DESCRIPTION
A library for sending MG-RAST logging to syslog.
METHODS
init_mlog(): Initializes mlog. It's good to call this at the beginning of your program.
logit(level, component, message, error_code): sends mgrast log message to syslog.
* level: (0-6) The logging level for this message is compared to the logging level that has been set in
mlog. If it is <= the set logging level, the message will be sent to syslog, otherwise it will be
ignored. Logging level is set to 6 if MG-RAST control API cannot be reached.
* component: (string) This is the utility within MG-RAST that is logging the message. This is a free
text field.
* message: (string) This is the log message.
* error_code: (string) The error code for this log message.
set_log_level(integer level, string component) : Sets the logging level of the given component. Only use this if
you wish to override the log levels that are defined by the control API.
* level : priority
* 0 : emergencies - vital component is down
* 1 : alerts - non-vital component is down
* 2 : errors - error that prevents proper operation
* 3 : warning - error, but does not prevent operation
* 4 : debug - lowest level of debug
* 5 : debug2 - second level of debug
* 6 : debug3 - highest level of debug
set_log_msg_check_count(integer count): used to set the number the messages that mlog will log before querying
the control API for the log level of all components (default is 100 messages).
set_log_msg_check_interval(integer seconds): used to set the interval, in seconds, that will be allowed to pass
before mlog will query the control API for the log level of the given component (default is 300 seconds).
use_all_api_log_levels() : Removes all user-defined log levels and tells mlog to use the control API defined log
levels.
use_api_log_level(string component) : Removes the user-defined log level for this component and tells mlog to use
the control API defined log levels.
"""

import datetime, re

#__all__ = ["mlog", "set_log_level", "set_log_msg_check_count", "set_log_msg_check_interval", "logit", "use_all_api_log_levels", "use_api_log_level"]

MLOG_CONF_FILE = "/etc/mlog/mlog.conf"
DEFAULT_LOG_LEVEL = 6
MSG_CHECK_COUNT = 100
MSG_CHECK_INTERVAL = 300 # 300s = 5min
LOG_LEVEL_TEXT = [ 'emerg', 'alert', 'crit', 'err',
'warning', 'notice', 'info', 'debug' ]

api_defined_log_levels = {}
user_defined_log_levels = {}
msg_count = 0
last_update_time = ""
last_update_msg_count = 0

class mlog():
"""
This class contains the methods necessary for sending MG-RAST log messages.
"""

def __init__(self):
self.last_update_msg_count = 0
self.last_update_time = datetime.datetime.now()

api_mlog_url = ""
for line in open(MLOG_CONF_FILE):
line.strip()
if(re.match(r'^url\s+', line)):
api_mlog_url = line.split()[1]

if(api_mlog_url != ""):
import json
import urllib2

data = json.load(urllib2.urlopen(api_mlog_url))
for components in data['components']:
api_defined_log_levels[components['name']] = components['log_level']

def _get_log_level(self, component):
if(component in api_defined_log_levels):
return api_defined_log_levels[component]
elif(component in user_defined_log_levels):
return user_defined_log_levels[component]
else:
return DEFAULT_LOG_LEVEL

0 comments on commit b99d26b

Please sign in to comment.