Icinga check_http via Proxy Server - http

I can't figure out how to use the check_http module of Icinga to use a http proxy.
I tried to achieve this using the following entry in hosts.conf.
object Host "host.local.ch" {
import "generic-host"
address = "192.168.200.20"
vars.http_vhosts["http"] = {
http_uri = "/"
http_proxy = "127.0.0.1"
http_proxy_port = 5016
}
}

I found a script, edited it and create files for the example usage with Icinga, find it here:
https://github.com/ozzi-/icinga-check-http-proxy
Save the following script (/etc/icinga2/scripts/check_http_proxy.sh):
#!/bin/bash
# Author: ozzi- , forked from scott.liao (https://github.com/shazi7804/icinga-check-http-proxy)
# Description: ICINGA2 http check with proxy support
# startup checks
if [ -z "$BASH" ]; then
echo "Please use BASH."
exit 3
fi
if [ ! -e "/usr/bin/which" ]; then
echo "/usr/bin/which is missing."
exit 3
fi
wget=$(which wget)
if [ $? -ne 0 ]; then
echo "Please install wget."
exit 3
fi
# Default Values
ssl=""
useragent=""
host=""
port=""
proxy=""
url="/"
times=1
timeout=5
warning=700
critical=2000
certificate=""
bindaddress=""
#set system proxy from environment
getProxy() {
if [ -z "$1" ]; then
echo $http_proxy | awk -F'http://' '{print $2}'
else
echo $https_proxy | awk -F'http://' '{print $2}'
fi
}
# Usage Info
usage() {
echo '''Usage: check_http_proxy [OPTIONS]
[OPTIONS]:
-p PORT Port to connect to (default: 80)
-u URL URL path (default: /)
-H HOSTNAME Destination Hostname
-a USERAGENT Sends a useragent and mimics other request headers of a browser
-s Use HTTPS proxy (default connecting to proxy via http)
-P PROXY Sets the proxy ip:port (i.e. 127.0.0.1:8840)
-w WARNING warning threshold in milliseconds (default: 700)
-c CRITICAL Critical threshold in milliseconds (default: 2000)
-n TRIES Number of connection attempts (default: 1)
-t TIMEOUT Seconds to wait for connection (timeout) (default: 5)
-C CERTIFICATE Path to a client certificate (PEM and DER file types supported)
-b IP Bind address for wget (default: IP of primary networking interface)'''
}
# Check which threshold was reached
checkTime() {
if [ $1 -gt $critical ]; then
echo -n "CRITICAL"
elif [ $1 -gt $warning ]; then
echo -n "WARNING"
else
echo -n "OK"
fi
}
# Return code value
getStatus() {
if [ $1 -gt $critical ]; then
return 2
elif [ $1 -gt $warning ]; then
return 1
else
return 0
fi
}
#main
#get options
while getopts "c:p:s:a:w:u:P:H:n:t:C:b:" opt; do
case $opt in
c)
critical=$OPTARG
;;
p)
port=$OPTARG
;;
s)
ssl=1
;;
a)
useragent=$OPTARG
;;
w)
warning=$OPTARG
;;
u)
url=$OPTARG
;;
P)
proxy=$OPTARG
;;
H)
hostname=$OPTARG
;;
n)
times=$OPTARG
;;
t)
timeout=$OPTARG
;;
C)
client_certificate=$OPTARG
;;
b)
bindaddress=$OPTARG
;;
*)
usage
exit 3
;;
esac
done
#define host with last parameter
host=$hostname
#hostname is required
if [ -z "$host" ] || [ $# -eq 0 ]; then
echo "Error: host is required"
usage
exit 3
fi
#set proxy from environment if available and no proxy option is given
if [ -z "$proxy" ]; then
proxy="$(getProxy ssl)"
fi
#use ssl or not
if [ -z "$ssl" ]; then
header="HTTP"
proxy_cmd="http_proxy=$proxy"
url_prefix="http://"
else
header="HTTPS"
proxy_cmd="https_proxy=$proxy"
url_prefix="https://"
fi
#different port
if [ -z "$port" ]; then
url="${url_prefix}${host}${url}"
else
url="${url_prefix}${host}:${port}${url}"
fi
start=$(echo $(($(date +%s%N)/1000000)))
if [ -z "$useragent" ]; then
if [ -z "$client_certificate" ]; then
#execute and capture execution time and return status of wget
$wget -t $times --timeout $timeout -O /dev/null -q -e $proxy_cmd --bind-address=${bindaddress} $url
status=$?
elif [ -n "$client_certificate" ]; then
#execute and capture execution time and return status of wget with client certificate
$wget -t $times --timeout $timeout -O /dev/null -q -e $proxy_cmd --bind-address=${bindaddress} --certificate=$client_certificate $url
status=$?
fi
else
if [ -n "$client_certificate" ]; then
$wget -t $times --timeout $timeout -O /dev/null -q -e $proxy_cmd --bind-address=${bindaddress} --certificate=$client_certificate $url \
--header="User-Agent: $useragent" \
--header="Accept: image/png,image/*;q=0.8,*/*;q=0.5" \
--header="Accept-Language: en-us,en;q=0.5" \
--header="Accept-Encoding: gzip, deflate"
status=$?
else
#execute with fake user agent and capture execution time and return status of wget
$wget -t $times --timeout $timeout -O /dev/null -q -e $proxy_cmd --bind-address=${bindaddress} $url \
--header="User-Agent: $useragent" \
--header="Accept: image/png,image/*;q=0.8,*/*;q=0.5" \
--header="Accept-Language: en-us,en;q=0.5" \
--header="Accept-Encoding: gzip, deflate"
status=$?
fi
fi
end=$(echo $(($(date +%s%N)/1000000)))
#decide output by return code
if [ $status -eq 0 ] ; then
echo "${header} $(checkTime $((end - start))): $((end - start))ms - ${url}|time=$((end - start))ms;${warning};${critical};0;"
getStatus $((end - start))
exit $?
else
case $status in
1)
echo "${header} CRITICAL: Generic error code ($status) - ${url}"
;;
2)
echo "${header} CRITICAL: Parse error ($status) - ${url}"
;;
3)
echo "${header} CRITICAL: File I/O error ($status) - ${url}"
;;
4)
echo "${header} CRITICAL: Network failure ($status) - ${url}"
;;
5)
echo "${header} CRITICAL: SSL verification failure ($status) - ${url}"
;;
6)
echo "${header} CRITICAL: Authentication failure ($status) - ${url}"
;;
7)
echo "${header} CRITICAL: Protocol errors ($status) - ${url}"
;;
8)
echo "${header} CRITICAL: Server issued an error response ($status) - ${url}"
;;
*)
echo "${header} UNKNOWN: $status - ${url}"
exit 3
;;
esac
exit 2
fi
Icinga command definition (/etc/icinga2/conf.d/commands.conf):
object CheckCommand "check-http-proxy" {
command = [ ConfigDir + "/scripts/check_http_proxy.sh" ]
arguments += {
"-p" = {
value = "$chp_port$"
description = "Port to connect to (default: 80)"
}
"-u" = {
value = "$chp_url$"
description = "URL path (default: /)"
}
"-H" = {
required = true
value = "$chp_hostname$"
description = "Destination Hostname"
}
"-s" = {
value = "$chp_ssl$"
description = "Use HTTPS proxy (default: http proxy)"
}
"-P" = {
required = true
value = "$chvp_proxy$"
description = "Sets the proxy ip:port (i.e. 127.0.0.1:8840)"
}
"-a" = {
value = "$chp_useragent$"
description = "Sends a useragent and mimics other request headers of a browser"
}
"-w" = {
value = "$chp_warning_timeout$"
description = "Warning threshold in milliseconds (default: 700)"
}
"-c" = {
value = "$chp_critical_timeout$"
description = "Critical threshold in milliseconds (default: 2000)"
}
"-b" = {
value = "$chp_bind_adr$"
description = "Bind address for wget (default: IP of primary networking interface)"
}
"-n" = {
value = "$chp_tries$"
description = "Number of connection attempts (default: 1)"
}
"-t" = {
value = "$chp_timeout$"
description = "Seconds to wait for connection (timeout) (default: 5)"
}
"-C" = {
value = "$chp_certificate$"
description = "Path to a client certificate (PEM and DER file types supported)"
}
}
}
Usage in /etc/icinga2/conf.d/hosts.conf
object Host "sub.domain.ch" {
check_command = "check-http-proxy"
vars.chp_hostname = "sub.domain.ch"
vars.chp_proxy = "127.0.0.1:5016"
}

Related

test ./script.sh doit test dont show true output

#!/usr/bin/env zsh
doit() {
if [[ "$1" = "start" ]]; then
for loc in $(cat all-doc); do
if ! screen -list | grep -q My-$loc; then
screen -dmS My-$loc /home/Server -f /home/$loc.cfg
fi
done
elif [[ "$1" = "stop" ]]; then
for loc in $(cat all-doc); do
if screen -list | grep -q My-$loc; then
pkill -f My-$loc;
fi
done
else
echo "Option: ERROR..."
fi
}
nothing() {
if [[ "$1" = "start" ]]; then
echo "Option: 1"
elif [[ "$1" = "stop" ]]; then
echo "Option: 2"
else
echo "Option: 3"
fi
}
case "$2" in
start)
"$1" "$2";
;;
stop)
"$1" "$2";
;;
restart)
restart;
;;
*)
echo "Usage: $0 {doit|nothing} {start|stop|restart}"
exit 1
;;
esac
exit 0
Output:
./script.sh:34> case test (start)
./script.sh:34> case test (stop)
./script.sh:34> case test (restart)
./script.sh:34> case test (*)
./script.sh:45> echo 'Usage: ./script.sh {start|stop|restart}'
sage: ./script.sh {start|stop|restart}
./script.sh:46> exit 1
this script for start and stop and restart my servers.
If $2 not match with "start" "stop" "restart" in both function must call else but not work.
Ok question is why ./script.sh doit test did not call
else
echo "Option: ERROR..."
whats the sulotion ? Is there better way to do somthing for ./script.sh $1 $2 i mean $1 get function and $2 get start|stop|restart ?

upload and download files from ftp server using Unix script

Below is the code that was used when we used .netrc file for automatic login. But now we can't use auto login because of multiprotocol environment.So have to manually read the .netrc file and fetch username and password.This is generic download script will download files from server. I need some help in converting this script to read the file and fetch the username and password.
I have added the code i used when auto login was used. I need to now read the file and fetch username and password and use that in the script. Below is format of .netrc file machine ftp.test login test1 password test2 .I need to read ftp.test from my script and fetch test1(username) and test2(password) to do ftp.
. $HOME/env
. $LIB_PATH/miip_functions.shl
OPTIND=1;ftpop=;user=;hosts=;quote=
while getopts h:f:n:q: arg
do
case $arg in
h) hosts="$OPTARG"
;;
f) hosts=`cat $OPTARG`
;;
n) ftpop=-n
user="user $OPTARG"
;;
q) quote="$OPTARG"
;;
\?) logMessage ERROR "download.shl was used incorrectly."
endRun 1
;;
esac
done
shift `expr $OPTIND - 1`
if [ $# -ne 2 ] ; then
logMessage ERROR "download.shl was used incorrectly."
endRun 1
fi
dataset="'$1'"
filename=$2
file=`basename $2`
if [ -z "$hosts" ] ; then
hosts=`cat $LIB_PATH/ftp.hosts 2> /dev/null`
if [ -z "$hosts" ] ; then
hosts="ftp.test ftp.test2"
fi
fi
logMessage DLOAD "Starting FTP download of $file."
for host in $hosts
do
ftp -v $ftpop $host << ! > $TMPFILE.ftp 2>&1
$user
$quote
get $dataset $filename
!
egrep '^421 |^425 |^426 |^450 |^451 |^452 |^530 |^531 |^550 |^551
|^552|^553 |^590 |^Not connected' $TMPFILE.ftp > /dev/null 2>&1
rtn=$?
if [ $rtn -eq 1 ] ; then
break
fi
done
(echo ; echo -------------- ; echo $PROGNAME ; echo --------------) >> $RUNFILE
cat $TMPFILE.ftp >> $RUNFILE
rm -f $TMPFILE.ftp
if [ $rtn -eq 1 ] ; then
logMessage DLOAD "Completed FTP download of $file."
else
logMessage ERROR "Download of $file failed."
fi
`

Unix run a script with -help option

I have the below script that is expected to work when the user invokes sh <scriptName> <propertyfile> It does work when I provide this at the dollar prompt. However, I am having two issues with the script.
If I provide just one argument, ie if I do - sh <scriptName>, I see the below error -
my-llt-utvsg$ sh temp.sh
Usage temp.sh
When I do -help, I see the below error -
my-llt-utvsg$ sh tmp.sh -help
-help does not exist
What am I doing wrong? Can someone please advise? I am a software developer that very rarely needs to do shell scripting, so please go easy on me ;)
#!/bin/bash
FILE="system.properties"
FILE=$1
if [ ! -f $FILE ];
then
echo "$FILE does not exist"
exit
fi
usage ()
{
echo "Usage $0 $FILE"
exit
}
if [ "$#" -ne 1 ]
then
usage
fi
if [ "$1" = "-help" ] ; then
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
exit
The reason your
if [ "$1" = "-help" ] ; then
check is not working is that it only checks $1 or the first argument.
Try instead:
for var in "$#"
do
if [ "$var" = "-help" ] ; then
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
fi
done
Which will loop over each argument and so will run if any of them are -help.
Try this as well:
#!/bin/bash
FILES=()
function show_help_info_and_exit {
echo ""
echo '############ HELP PROPERTIES ############ '
echo ""
echo 'Blah.'
exit
}
function show_usage_and_exit {
echo "Usage: $0 file"
exit
}
for __; do
if [[ $__ == -help ]]; then
show_help_info_and_exit
elif [[ -f $__ ]]; then
FILES+=("$__")
else
echo "Invalid argument or file does not exist: $__"
show_usage_and_exit
fi
done
if [[ ${#FILES[#]} -ne 1 ]]; then
echo "Invalid number of file arguments."
show_usage_and_exit
fi
echo "$FILES"

Unix troubleshooting, missing /etc/init.d file

I am working through this tutorial on daemonizing php scripts. When I run the following Unix command:
. /etc/init.d/functions
#startup values
log=/var/log/Daemon.log
#verify that the executable exists
test -x /home/godlikemouse/Daemon.php || exit 0RETVAL=0
prog="Daemon"
proc=/var/lock/subsys/Daemon
bin=/home/godlikemouse/Daemon.php
start() {
# Check if Daemon is already running
if [ ! -f $proc ]; then
echo -n $"Starting $prog: "
daemon $bin --log=$log
RETVAL=$?
[ $RETVAL -eq 0 ] && touch $proc
echo
fi
return $RETVAL
}
I get the following output:
./Daemon: line 12: /etc/init.d/functions: No such file or directory
Starting Daemon: daemon: unrecognized option `--log=/var/log/Daemon.log'
I looked at my file system and there was no /etc/init.d file. Can anyone tell me what this is and where to obtain it? Also is the absence of that file what's causing the other error?
Separate your args within their own " " double-quotes:
args="--node $prog"
daemon "nohup ${exe}" "$args &" </dev/null 2>/dev/null
daemon "exe" "args"

Init.d script for nginx on Cent OS [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm using the init.d script from this: http://wiki.nginx.org/RedHatNginxInitScript but this script sucks on my CentOS 5.5 nginx 1.0.6 (which installed by passenger).
It sucks for 2 reasons:
When I run service nginx start, it start nginx but then do not quit it self.
It run in some different runtime path ($PATH) so that rails won't be able to find executables located inside /usr/local/bin (like node.js)
Is there any init.d script that works for you guys (on CentOS or other Redhat based distribution) that does not have these problems?
The one here: http://articles.slicehost.com/2009/2/2/centos-adding-an-nginx-init-script has worked for me in the past.
You will need to change the paths to "/etc/nginx" instead of "/usr/local/nginx" for instance.
Actually both are similar except that your old one has an additional "MakeDirs" function. Not sure why this may this be needed.
My current one is below:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
sysconfig="/etc/sysconfig/$prog"
lockfile="/var/lock/subsys/nginx"
pidfile="/var/run/${prog}.pid"
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f $sysconfig ] && . $sysconfig
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $pidfile $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest_q || return 6
stop
start
}
reload() {
configtest_q || return 6
echo -n $"Reloading $prog: "
killproc -p $pidfile $prog -HUP
echo
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
$nginx -t -q -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
# Upgrade the binary with no downtime.
upgrade() {
local oldbin_pidfile="${pidfile}.oldbin"
configtest_q || return 6
echo -n $"Upgrading $prog: "
killproc -p $pidfile $prog -USR2
retval=$?
sleep 1
if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
killproc -p $oldbin_pidfile $prog -QUIT
success $"$prog online upgrade"
echo
return 0
else
failure $"$prog online upgrade"
echo
return 1
fi
}
# Tell nginx to reopen logs
reopen_logs() {
configtest_q || return 6
echo -n $"Reopening $prog logs: "
killproc -p $pidfile $prog -USR1
retval=$?
echo
return $retval
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest|reopen_logs)
$1
;;
force-reload|upgrade)
rh_status_q || exit 7
upgrade
;;
reload)
rh_status_q || exit 7
$1
;;
status|status_q)
rh_$1
;;
condrestart|try-restart)
rh_status_q || exit 7
restart
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
exit 2
esac
This is what is distributed with the Nginx RPM from EPEL.

Resources