#!/bin/bash
# ----------------------------------------------------------------------
# File: eos-prom-push
# Author: Andreas-Joachim Peters - CERN
# ----------------------------------------------------------------------

# ************************************************************************
# * EOS - the CERN Disk Storage System                                   *
# * Copyright (C) 2024 CERN/Switzerland                                  *
# *                                                                      *
# * This program is free software: you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
# * the Free Software Foundation, either version 3 of the License, or    *
# * (at your option) any later version.                                  *
# *                                                                      *
# * This program is distributed in the hope that it will be useful,      *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
# * GNU General Public License for more details.                         *
# *                                                                      *
# * You should have received a copy of the GNU General Public License    *
# * along with this program.  If not, see <http://www.gnu.org/licenses/>.*
# ************************************************************************

# ************************************************************************
# the push endpoint can be overwritten in /etc/sysconfig/eos_prom
# EOS_PROM_URL=...
# the push can be disabled in /etc/sysconfig/eos_prom
# EOS_PROM_DISABLE=1
# ************************************************************************
# By default the push is enabled!
# ************************************************************************

# ************************************************************************
# just check if we have an MGM locally, otherwise we don't do anything
config=$(timeout 5 eos root://localhost version 2>/dev/null)
test $? -ne 0 && exit 0
export $config
EOS_PROM_URL="http://eos-prometheus.cern.ch:9091/metrics/job/user_inspector/instance"
EOS_PROM_CLIENT=`hostname -f`
test -e "/etc/sysconfig/eos_prom" && . /etc/sysconfig/eos_prom

if [ "${EOS_PROM_DISABLE}" == "1" ]; then
    exit 0;
fi    

# ************************************************************************
# Publish only files created in the last 5 minutes window
for name in `find /var/eos/md -type f -cmin -5 -name "*.prom"`; do
    cat $name | timeout 10 curl --data-binary @- ${EOS_PROM_URL}/${EOS_PROM_CLIENT}/cluster/${EOS_INSTANCE} 
done


