Making varnish log time_firstbyte and handling - varnish

I am trying to log time_firstbyte and handling in a file via varnishncsa.
My /etc/init.d/varnishncsa looks like below:-
. /lib/lsb/init-functions
NAME=varnishncsa
DESC="HTTP accelerator log deamon"
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/$NAME
PIDFILE=/run/$NAME/$NAME.pid
LOGFILE=/var/log/varnish/varnishncsa.log
USER=root
LOG_FORMAT="%t %r %s %b %{Varnish:time_firstbyte}x %{Varnish:handling}x"
#VARNISHNCSA_ENABLED=1
DAEMON_OPTS="-a -w ${LOGFILE} -D -P ${PIDFILE} -F '${LOG_FORMAT}'"
# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
. /etc/default/$NAME
fi
# If unset, or set to "0" or "no", exit
if [ -z "${VARNISHNCSA_ENABLED}" ] || \
[ "${VARNISHNCSA_ENABLED}" = "0" ] || \
[ "${VARNISHNCSA_ENABLED}" = "no" ]; then
exit 0;
fi
test -x $DAEMON || exit 0
start_varnishncsa() {
output=$(/bin/tempfile -s.varnish)
log_daemon_msg "Starting $DESC" "$NAME"
create_pid_directory
if start-stop-daemon --start --quiet --pidfile ${PIDFILE} \
--chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} \
> ${output} 2>&1; then
log_end_msg 0
else
log_end_msg 1
cat $output
exit 1
fi
rm $output
}
stop_varnishncsa(){
log_daemon_msg "Stopping $DESC" "$NAME"
if start-stop-daemon --stop --quiet --pidfile $PIDFILE \
--retry 10 --exec $DAEMON; then
log_end_msg 0
else
log_end_msg 1
fi
}
reload_varnishncsa(){
log_daemon_msg "Reloading $DESC" "$NAME"
if kill -HUP $(cat $PIDFILE) >/dev/null 2>&1; then
log_end_msg 0
else
log_end_msg 1
exit 1
fi
}
status_varnishncsa(){
status_of_proc -p "${PIDFILE}" "${DAEMON}" "${NAME}"
exit $?
}
create_pid_directory() {
install -o $USER -g $USER -d $(dirname $PIDFILE)
}
case "$1" in
start)
start_varnishncsa
;;
stop)
stop_varnishncsa
;;
reload)
reload_varnishncsa
;;
status)
status_varnishncsa
;;
restart|force-reload)
$0 stop
$0 start
;;
*)
log_success_msg "Usage: $0 {start|stop|restart|force-reload|reload}"
exit 1
;;
esac
My /etc/default/varnishncsa looks like below:-
VARNISHNCSA_ENABLED=1
But while trying to do restart varnishncsa it is failing.
I am on ubuntu 12.04 with varnish 4.1.

As per this link updating my /etc/init.d/varnishncsa as below solved the issue:-
LOG_FORMAT="%h|%u|%{%Y-%m-%d}t|%{%H:%M:%S}t|%{%z}t|%m|%{Host}i|%U|%q|%s|%b|%{Referer}i|%{User-agent}i|%{Varnish:time_firstbyte}x|%T|%D|%{Varnish:handling}x|%{X-FE-Varnish-Obj-TTL}o|%{X-FE-Varnish-Backend}o|%{X-FE-Varnish-Obj-Stat}o"
DAEMON_OPTS="-a -w ${LOGFILE} -D -P ${PIDFILE} -F '${LOG_FORMAT}'"

The %T format specifier can be tricky if you are also using ntp. Because the response time can be very small - you may end up with negative values in your log file if the clock skew changes in the middle of the request. If you are post-processing your varnishncsa logs you may end up with results you were not expecting.

Related

Create init.d script file

I have a sample file with this content:
#!/bin/bash
# Setting environment
CLASSPATH="."
CLASSPATH="$CLASSPATH:props"
CLASSPATH="$CLASSPATH:cfg"
CLASSPATH="$CLASSPATH:./bin/*"
CLASSPATH="$CLASSPATH:./wslib/*"
CLASSPATH="$CLASSPATH:./oalib/*"
JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF8"
# RAM usage settings
JAVA_OPTS="$JAVA_OPTS -Xms1024m -Xmx1024m"
# Settings of GC
JAVA_OPTS="$JAVA_OPTS -XX:ParallelGCThreads=2"
# Print of memory usage
JAVA_OPTS="$JAVA_OPTS -Xloggc:./log/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution"
# setting config
JAVA_OPTS="$JAVA_OPTS se.highex.ebe.adaptor.Adapter -config cfg/config_gw_ws.xml"
echo $CLASSPATH
echo $JAVA_OPTS
export CLASSPATH
export JAVA_OPTS
java $JAVA_OPTS
file is locating in /app/somepath/stpa.sh
how I can create an init.d file for start/stop/restart it?
I found this tutorial, but after service stpa start command there is no any messages in terminal and I think, that my script is not working. But can't find the reason.
P.S. Sorry for my bad English!
EDIT:
this is my stpa file in /etc/init.d
#!/bin/sh
#
# chkconfig: 2345 90 60
name="STPA_WS"
command="/app/STPAdapterPG/stpa_ws.sh"
command_args=""
daemon="/usr/local/bin/daemon"
[ -x "$daemon" ] || exit 0
[ -x "$command" ] || exit 0
daemon_start_args=""
pidfiles="/var/run"
user=""
chroot=""
chdir=""
umask=""
stdout="daemon.info"
stderr="daemon.err"
case "$1" in
start)
if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
then
echo "$name is already running."
else
echo -n "Starting $name..."
"$daemon" --respawn $daemon_start_args \
--name "$name" --pidfiles "$pidfiles" \
${user:+--user $user} ${chroot:+--chroot $chroot} \
${chdir:+--chdir $chdir} ${umask:+--umask $umask} \
${stdout:+--stdout $stdout} ${stderr:+--stderr $stderr} \
-- \
"$command" $command_args
echo done.
fi
;;
stop)
if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
then
echo -n "Stopping $name..."
"$daemon" --stop --name "$name" --pidfiles "$pidfiles"
echo done.
else
echo "$name is not running."
fi
;;
restart|reload)
if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
then
echo -n "Restarting $name..."
"$daemon" --restart --name "$name" --pidfiles "$pidfiles"
echo done.
else
echo "$name is not running."
exit 1
fi
;;
status)
"$daemon" --running --name "$name" --pidfiles "$pidfiles" --verbose
;;
*)
echo "usage: $0 <start|stop|restart|reload|status>" >&2
exit 1
esac
exit 0
I think you did not complete the init.d script format, you need to add the content just like the tutorial you mentioned:
case "$1" in
start)
#here do something you need when start
;;
stop)
#here do something you need when stop
;;
restart|reload)
;;
status)
;;
*)
echo "usage: $0 <start|stop|restart|reload|status>" >&2
exit 1
esac

initscript CentOS launching chroot nginx problems

I found this initscript for launching nginx here: http://www.rackspace.com/knowledge_center/article/centos-adding-an-nginx-init-script. Im trying to modify it to work with a chroot environment and its proving more troublesome than I expected.
Here's what I have:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog="nginx"
chroot="/usr/sbin/chroot /nginx
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $chroot $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killall $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killall $prog -HUP
retval=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
if rh_status_q; then
printf "$prog already running\n"
exit 0
fi
$1
;;
stop)
if ! rh_status_q; then
printf "$prog not running\n"
exit 0
fi
$1
;;
restart|configtest)
$1
;;
reload)
if ! rh_status_q; then
printf "$prog not running\n"
exit 7
fi
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
if ! rh_status_q; then
printf "$prog not running\n"
exit 0
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
The main thing not working right now is the status call.
[root#localhost ~]# /etc/init.d/nginx status
nginx dead but subsys locked
This error points to a lockfile being present but no pid file. This is true, however when I use the original nginx init script and launch it in a non-chroot environment, there is no pidfile, and the status call works just fine.
I put in some lines to retrieve the pid by launching the process in the background instead of using daemon:
PID=$chroot $nginx -c $NGINX_CONF_FILE > /dev/null 2>&1 $ echo $!
echo $PID > $pidfile
But this grabs the pid of chroot, which ends after nginx is launched. Nginx has both a master and a child pid. Might grabbing those and putting them to a pidfile be the way forward? Or is there something else I should try?
I think its because daemon launches a chroot process it gets the wrong pid.
Alright it was as easy as grepping for the pid
pidfile='/var/run/nginx.pid'
start(){
...
daemon $chroot $nginx -c $NGINX_CONF_FILE
PID=`ps -ef | grep $nginx | grep -v grep | awk '{print $2}'`
echo $PID > $pidfile
...
}
stop() {
...
[ $retval -eq 0 ] && rm -f $lockfile && rm -f $pidfile
return $retval
}

Service status not working

I have the following code for a service that I'm trying to have automatically start on boot.
#!/bin/sh
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
prog='foo'
exec="/usr/sbin/$prog"
pidfile="/var/run/$prog.pid"
lock_file="/var/lock/subsys/$prog"
logfile="/var/log/$prog"
if [ -f /etc/default/foo ]; then
. /etc/default/foo
fi
if [ -z $QUEUE_TYPE ]; then
echo 'ENV variable QUEUE_TYPE has not been set, please set it in /etc/default/foo'
exit 1
fi
get_pid() {
cat "$pidfile"
}
is_running() {
[ -f "$pidfile" ] && ps `get_pid` > /dev/null 2>&1
}
case "$1" in
start)
echo -n "Starting Consul daemon: "
#
daemon --pidfile $pidfile --check foo --user my-user "my app stuff here"
echo
;;
stop)
echo -n 'Stopping Consul daemon: '
killproc foo
echo
;;
status)
status $pidfile
RETVAL=$?
#status -p $pidfile -l $prog
#[ $RETVAL -eq 0 ] && RETVAL=$?
#RETVAL=$?
#if is_running; then
# echo 'Running'
#else
# echo 'Not Running'
#fi
#status foo
#RETVAL=$?
;;
restart)
$0 stop
$0 start
RETVAL=$?
;;
*)
echo 'Usage: foo {start|stop|status|restart}'
exit 1
esac
exit $RETVAL
When I run sudo service foo status it says that it hasn't been started which is correct. After running sudo service foo start and then running the status command, it tells me that the service hasn't been started. I'm not sure what is causing this to happen. I looked at the configurations for other init.d scripts to see how they were handling this and tried to follow their lead. Is there something obvious here that I'm doing wrong or something else that I may be unaware of that's causing this problem?

not to run duplicate instances of service

I am writing a init.d script for kibana
as of not script is running partially, but the issue is if I run run service kibana start even if service is running then second instance start which bothers me I want to add check before starting service, if service is running then dont start second instance. I tried to put if check on "/var/lock/subsys/kibana" but didn't work. Here is my script :
#!/bin/bash
KIBANA_PATH="/opt/kibana4"
DESC="Kibana Daemon"
NAME=kibana
DAEMON=bin/kibana
CONFIG_DIR=$KIBANA_PATH/config/kibana.yml
LOGFILE=/var/log/kibana/kibana.log
#ARGS="agent --config ${CONFIG_DIR} --log ${LOGFILE}"
SCRIPTNAME=/etc/init.d/kibana
PIDFILE=/var/run/kibana.pid
base=kibana
# Exit if the package is not installed
if [ ! -x "$KIBANA_PATH/$DAEMON" ]; then
{
echo "Couldn't find $DAEMON"
exit 99
}
fi
. /etc/init.d/functions
#
# Function that starts the daemon/service
#
do_start()
{
cd $KIBANA_PATH && \
($DAEMON >> $LOGFILE &) && \
success || failure;
}
set_pidfile()
{
pgrep -f "kibana.jar" > $PIDFILE
}
#
# Function that stops the daemon/service
#
do_stop()
{
pid=`cat $PIDFILE`
if checkpid $pid 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid >/dev/null 2>&1
usleep 100000
if checkpid $pid && sleep 1 &&
checkpid $pid && sleep $delay &&
checkpid $pid ; then
kill -KILL $pid >/dev/null 2>&1
usleep 100000
fi
fi
checkpid $pid
RC=$?
[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
}
case "$1" in
start)
echo -n "Starting $DESC: "
do_start
touch /var/lock/subsys/$NAME
set_pidfile
;;
stop)
echo -n "Stopping $DESC: "
do_stop
rm /var/lock/subsys/$NAME
rm $PIDFILE
;;
restart|reload)
echo -n "Restarting $DESC: "
do_stop
do_start
touch /var/lock/subsys/$NAME
set_pidfile
;;
status)
echo $DESC
status -p $PIDFILE
echo $!
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac
echo
exit 0
any help here ?
Thanks
use lockfile -r0 /path/to/lock/file.lck when you start the service. every new access then will retry zero times to create the file. so if that command fails do nothing or start the service otherwise.
lockfile -r0 /path/to/lock/file.lck
if [ "$?" == "0" ]; then
echo "lock does not exist. enter devils land :)"
fi
The following is a pretty standard implementation of this feature used by most init.d scripts.
start () {
[ -d /var/run/nscd ] || mkdir /var/run/nscd
[ -d /var/db/nscd ] || mkdir /var/db/nscd
echo -n $"Starting $prog: "
daemon /usr/sbin/nscd $NSCD_OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/nscd
return $RETVAL
}
...
# See how we were called.
case "$1" in
start)
[ -e /var/lock/subsys/nscd ] || start
RETVAL=$?
;;
...

Nginx won't start. /usr/sbin/nginx and /var/run/nginx.pid don't exist

I try to Nginx with /etc/init.d/nginx start. It exits with value 0 and otherwise does absolutely nothing because /usr/sbin/nginx doesn't exist.
Here is /etc/init.d/nginx:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then
. /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
# We made the awk expression more precise:
# https://code.google.com/p/phusion-passenger/issues/detail?id=1060
PID=$(awk -F'[ \t;]+' '/[^#]\<pid/ {print $2}' /etc/nginx/nginx.conf)
if [ -z "$PID" ]
then
PID=/var/run/nginx.pid
fi
# Check if the ULIMIT is set in /etc/default/nginx
if [ -n "$ULIMIT" ]; then
# Set the ulimits
ulimit $ULIMIT
fi
test_nginx_config() {
$DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
retvar=$?
if [ $retvar -ne 0 ]
then
exit $retvar
fi
}
start() {
start-stop-daemon --start --quiet --pidfile $PID \
--retry 5 --exec $DAEMON --oknodo -- $DAEMON_OPTS
}
stop() {
start-stop-daemon --stop --quiet --pidfile $PID \
--retry 5 --oknodo --exec $DAEMON
}
case "$1" in
start)
test_nginx_config
log_daemon_msg "Starting $DESC" "$NAME"
start
log_end_msg $?
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop
log_end_msg $?
;;
restart|force-reload)
test_nginx_config
log_daemon_msg "Restarting $DESC" "$NAME"
stop
sleep 1
start
log_end_msg $?
;;
reload)
test_nginx_config
log_daemon_msg "Reloading $DESC configuration" "$NAME"
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID \
--oknodo --exec $DAEMON
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
if test_nginx_config; then
log_daemon_msg "$NAME"
else
exit $?
fi
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" nginx
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2
exit 1
;;
esac
exit 0
I see that it exits with value 0 because of the test -x $DAEMON || exit 0 line. If I comment that line out then the script will stop suddenly inside of the test_nginx_config() function when it goes to run the daemon (/usr/sbin/nginx) which doesn't exist. There is no /usr/local/nginx/ directory either, for what it's worth.
I'm a total newbie at this stuff so there might be something dumb simple going on but I just need to get this started up. Any help would be greatly appreciated.
Well, if this is your start up script and nginx used to work with it then, Nginx has been removed by something or someone.

Resources