Exemplo n.º 1
0
def health_check(sub):
    configf = configparser.ConfigParser()
    configf.read('config.ini')
    configname = 'SysLastRun' + sub.display_name
    lastrunstr = configf['DEFAULT'][configname]

    check_log_errors(sub)

    with open(logmsg.handlers[0].baseFilename) as f:
        if 'Bot is in bad health state' in f.read():
            debug_msg(
                "Bot previously in a bad health state, not sending modmail alert again today."
            )
            return True

    if (datetime.utcnow() - datetime.strptime(
            lastrunstr, configf['DEFAULT']['lastrunformat'])) > timedelta(1):
        logmsg.critical(
            "[ALERT] Bot is in bad health state, sent modmail alert.")
        alert_mods(
            sub.display_name, 'Alert: bot failed to run',
            'Warning: This bot has failed to run at last attempt. If this repeats over multiple days, review the bot log files for more details.'
        )
        return False
    return True
Exemplo n.º 2
0
def main():
    if not re.match(r'^[A-Za-z0-9_]+$', args.subname):
        sys.exit("Invalid subreddit name, aborting.")
    s = reddit.reddit.subreddit(args.subname)
    common.debug_msg('Mod Permission: ' + str(s.user_is_moderator))
    if not s.user_is_moderator:
        logmsg.critical("[ERROR] Bot check as mod failed, aborting.")
        sys.exit("Shutting down due to bot permission issue.")
    checklog.check_for_admins(s)
    checklog.health_check(s)
    if not common.bool_sidebar_queued(s):
        sys.exit(
            "Shutting down due to no need to run bot, no new sidebar content found."
        )
    new_sidebar = common.sync_sidebar_widget(s)
    sidebar_state = common.check_sidebar_freespace(s.display_name, new_sidebar)
    if not debug_mode:
        try:
            s.mod.update(description=new_sidebar)
        except Exception as e:
            logmsg.critical("[ERROR] Updating sidebar - %s", e)
    common.debug_msg("Bot run has completed. API usage: " +
                     str(reddit.reddit.auth.limits))
    if not debug_mode:
        configname = 'SysLastRun' + s.display_name
        currentrun = datetime.utcnow().strftime(
            config['DEFAULT']['lastrunformat'])
        config['DEFAULT'][configname] = currentrun
        with open('config.ini', 'w') as configfile:
            config.write(configfile)
Exemplo n.º 3
0
def check_for_admins(sub):
    found = False
    configf = configparser.ConfigParser()
    configf.read('config.ini')
    configname = 'SysLastRun' + sub.display_name
    lastrunstr = configf['DEFAULT'][configname]
    lastrun = datetime.strptime(lastrunstr,
                                configf['DEFAULT']['lastrunformat'])
    lastday = lastrun.day
    currentday = datetime.utcnow().day
    if lastday == currentday:
        debug_msg("Not a new day, won't check modlog for admin activity.")
        return False

    bodystr = ""
    for log in sub.mod.log(limit=None, mod='a'):
        datestr = datetime.fromtimestamp(
            log.created_utc).strftime("%Y-%m-%d %H:%M:%S")
        if (datetime.utcnow() -
                datetime.fromtimestamp(log.created_utc)) < timedelta(1):
            bodystr = bodystr + "* " + str(
                log.mod
            ) + " action at " + datestr + " UTC against /u/" + log.target_author + ": action [" + log.action + "](" + log.target_permalink + ")\n"
            found = True

    if found:
        headerstr = "Admin activity was detected in the [modlog](https://www.reddit.com/r/" + sub.display_name + "/about/log/?mod=a). Review below to determine which Reddit content policy was violated, or if a false positive contact the admins.\n\n" + bodystr
        alert_mods(sub.display_name, 'Alert: admin activity in the modlog',
                   headerstr)

    return found
Exemplo n.º 4
0
def check_log_errors(sub):
    logmodified = os.path.getmtime(logmsg.handlers[0].baseFilename)
    if (datetime.utcnow() -
            datetime.fromtimestamp(logmodified)) < timedelta(1):
        debug_msg("Errors seen today.")
        return True
    else:
        debug_msg("No errors today.")
        return False
Exemplo n.º 5
0
def main():
    if not re.match(r'^[A-Za-z0-9_]+$', args.subname):
        sys.exit("Invalid subreddit name, aborting.")
    s = reddit.reddit.subreddit(args.subname)
    config = core.get_config()
    debug_mode = config['DEFAULT'].getboolean('DebugMode')
    common.debug_msg('Mod Permission: ' + str(s.user_is_moderator))
    if not s.user_is_moderator:
        logmsg.critical("[ERROR] Bot check as mod failed, aborting.")
        sys.exit("Shutting down due to bot permission issue.")
    checklog.check_for_admins(s)
    checklog.health_check(s)
    common.cleanup_modmail(s)
    if not common.bool_sidebar_queued(s):
        sys.exit(
            "Shutting down due to no need to run bot... no new sidebar content found."
        )
    new_sidebar = common.sync_sidebar_widget(s)
    sidebar_state = common.check_sidebar_freespace(s.display_name, new_sidebar)
    if not debug_mode:
        try:
            s.mod.update(description=new_sidebar)
        except Exception as e:
            logmsg.critical("[ERROR] Updating sidebar - %s", e)
    common.debug_msg("Bot run has completed.. API usage: " +
                     str(reddit.reddit.auth.limits))
    if not debug_mode:
        configf = configparser.ConfigParser()
        configf.read('config.ini')
        configname = 'SysLastRun' + s.display_name
        currentrun = datetime.utcnow().strftime(
            configf['DEFAULT']['lastrunformat'])
        configf['DEFAULT'][configname] = currentrun
        usern = "u_" + reddit.reddit.user.me().name
        statusmsg = "Bot last online " + currentrun + " UTC. "
        if checklog.check_log_errors(s):
            statusmsg = statusmsg + "❌Errors seen today."
        else:
            statusmsg = statusmsg + "✔️No errors today."
        reddit.reddit.subreddit(usern).mod.update(public_description=statusmsg)
        with open('config.ini', 'w') as configfile:
            configf.write(configfile)