#!/bin/bash

TSKV_UNIXTIME_TIMESTAMP='unixtime=(\d+)'
GUNICORN_TIMESTAMP='^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}) '
NGINX_ERROR_LOG_TIMESTAMP='^(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2})\.'
NGINX_ACCESS_LOG_TIMESTAMP='\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2} \+\d{4})\]'

collect_metrics() {
    TIMESTAMP_RE=$1
    CONF=$2
    LOG=$3
    TMP="/tmp/$(basename $LOG).last_sent_metrics"

    error "Collect metrics from $LOG"

    [ -r $CONF ] || {
        error "Unable to read from $CONF"
        return 1
    }

    [ -r $LOG ] || {
        error "Unable to read from $LOG"
        return 1
    }
    TMPDIR=`dirname $TMP`
    [ -w $TMPDIR ] || {
        error "Unable to write to $TMPDIR"
        return 1
    }
    [ ! -e $TMP -o -w $TMP ] || {
        error "Unable to write to $TMP"
        return 1
    }

    (
        flock -n 100 || {
            error "Unable to acquire the lock"
            exit 1
        }

        timetail -n $TIME -r "$TIMESTAMP_RE" $LOG | passport-metrics -c "$CONF"  > $TMP

        [ "x$DEBUG" != "x" ] && cat $TMP

        curl -d "@$TMP" --connect-timeout 5 'http://localhost:11005/' >/dev/null 2>&1 || {
            error "Unable to send metrics to Golovan"
            return 1
        }
    ) 100>$TMP
}


error() {
    [ "x$DEBUG" != "x" ] && echo $1 >&2
}


TIME=$1
CONF_DIR='/etc/yandex/social-metrics'
LOG_DIR='/var/log/yandex/socialism'
NGINX_LOG_DIR='/opt/www/social/log'

[ -n "$TIME" ] || {
    error "Syntax: $0 period"
    exit 1
}

collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-access-metrics.ini" "$LOG_DIR/social-api.access.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-access-metrics.ini" "$LOG_DIR/social-broker.access.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-access-metrics.ini" "$LOG_DIR/social-proxy2.access.log"

collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-exception-metrics.ini" "$LOG_DIR/social-api.exception.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-exception-metrics.ini" "$LOG_DIR/social-broker.exception.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-exception-metrics.ini" "$LOG_DIR/social-proxy2.exception.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-exception-metrics.ini" "$LOG_DIR/social-utils.exception.log"

collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-warning-metrics.ini" "$LOG_DIR/social-api.warning.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-warning-metrics.ini" "$LOG_DIR/social-broker.warning.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-warning-metrics.ini" "$LOG_DIR/social-proxy2.warning.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-warning-metrics.ini" "$LOG_DIR/social-utils.warning.log"

collect_metrics "$GUNICORN_TIMESTAMP" "$CONF_DIR/social-api-gunicorn-metrics.ini" "$LOG_DIR/social-api.gunicorn.error.log"
collect_metrics "$GUNICORN_TIMESTAMP" "$CONF_DIR/social-broker-gunicorn-metrics.ini" "$LOG_DIR/social-broker.gunicorn.error.log"
collect_metrics "$GUNICORN_TIMESTAMP" "$CONF_DIR/social-proxy2-gunicorn-metrics.ini" "$LOG_DIR/social-proxy2.gunicorn.error.log"

collect_metrics "$NGINX_ERROR_LOG_TIMESTAMP" "$CONF_DIR/social-nginx-error-log-metrics.ini" "$NGINX_LOG_DIR/nginx.error_log"
collect_metrics "$NGINX_ACCESS_LOG_TIMESTAMP" "$CONF_DIR/social-internal-nginx-access-log-metrics.ini" "$NGINX_LOG_DIR/nginx.api.social.yandex.ru.access_log"
collect_metrics "$NGINX_ACCESS_LOG_TIMESTAMP" "$CONF_DIR/social-external-nginx-access-log-metrics.ini" "$NGINX_LOG_DIR/nginx.social.yandex.ru.access_log"

collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-api-graphite-metrics.ini" "$LOG_DIR/social-api.graphite.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-broker-graphite-metrics.ini" "$LOG_DIR/social-broker.graphite.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-proxy2-graphite-metrics.ini" "$LOG_DIR/social-proxy2.graphite.log"
collect_metrics "$TSKV_UNIXTIME_TIMESTAMP" "$CONF_DIR/social-utils-graphite-metrics.ini" "$LOG_DIR/social-utils.graphite.log"

exit 0
