通过脚本验证tinproxy安装

时间:2015-11-28 21:11:51

标签: linux shell scripting ansible ansible-playbook

我在Ubuntu中创建代理并部署了tinyproxy。部署和配置完成后,必须重新启动。它将输出如下:

tail -f /var/log/tinyproxy/tinyproxy.log
INFO Nov 10 22:57:26 [2581]: Creating child number 4 of 10 ...
INFO Nov 10 22:57:26 [2581]: Creating child number 5 of 10 ...
INFO Nov 10 22:57:26 [2581]: Creating child number 6 of 10 ...
INFO Nov 10 22:57:26 [2581]: Creating child number 7 of 10 ...
INFO Nov 10 22:57:26 [2581]: Creating child number 8 of 10 ...
INFO Nov 10 22:57:26 [2581]: Creating child number 9 of 10 ...
INFO Nov 10 22:57:26 [2581]: Creating child number 10 of 10 ...
INFO Nov 10 22:57:26 [2581]: Finished creating all children.
INFO Nov 10 22:57:26 [2581]: Setting the various signals.
INFO Nov 10 22:57:26 [2581]: Starting main loop. Accepting connections

这是成功的方案。我需要使用已正确安装的shell脚本验证输出。我可以看到这个安装成功了。有任何建议验证这是否成功安装?
更新:
它必须通过ansible实现。我试过跟着

- name: Restarting tinyporxy
  service: name=tinyproxy state=restarted

- name: Verifying insllation
  command: tail /var/log/tinyproxy/tinyproxy.log 
  register: output
  failed_when:"'Starting main loop. Accepting connections' not in output.stdout"

相关
.....
 INFO Nov 10 22:57:26 [2581]: Creating child number 5 of 10 ...
 FATAL : all host have already failed

似乎在尾部完成之前已经完成了验证。我尝试使用tail -f它会自动卡在那里。

似乎需要在tail之前添加一些延迟。帮助我如何在这里添加延迟。

1 个答案:

答案 0 :(得分:1)

这是一种跟踪全新安装状态的方法。

cat verifyInstall.sh

#!/bin/ksh

chkFile=$1

if [[ ! -f "$chkFile" ]] ; then
  echo "can't find file=$chkFile, can't continue" >&2
  exit 1
fi

awk '
  /Creating child number 4/{c4=1}
  /Creating child number 5/{c5=1}
  /Creating child number 6/{c6=1}
  /Creating child number 7/{c7=1}
  /Creating child number 8/{c8=1}
  /Creating child number 9/{c9=1}
  /Creating child number 10/{c10=1}
  /Finished creating all children/{fin=1}
  /Setting the various signals/{sigs=1}
  /Starting main loop. Accepting connections/{start=1}
  {
    if (c4 && c5 && c6 && c7 && c8 && c9 && c10 && fin && sigs && start) {
      print "All steps completed, done monitoring script log file=" FILENAME
      exit 0
    }
  }

  END {
    if (c4 && c5 && c6 && c7 && c8 && c9 && c10 && fin && sigs && start) {
      x=1
      # nothing to do here really, except
    }
    else {
      print "Unnexpectedly leaving script validInstall.sh, not all vars as expected, found status=" \
        "c4=" c4 " c5=" c5 " c6=" c6 " c7=" c7 " c8=" c8 " c9=" c9 " c10=" c10  \
        " fin=" fin "  sigs=" sigs  " start=" start
  exit 1
    }
  }
' "$chkFile"

您当然知道,创建文件后,您需要使用chmod +x validInstall.sh使其可执行吗?

<强>输出

$ validInstall.sh tst.log
All steps completed, done monitoring script log file=tst.log

带有干净的日志文件

$ validInstall.sh badTst.log
Unnexpectedly leaving script validInstall.sh, not all vars as expected, found status=c4=1 c5=1 c6= c7=1 c8=1 c9=1 c10=1 fin=1  sigs=1 start=1

带有tst.log的副本,其中'child number 6'已更改为'child number 16'

因为您在日志文件中使用tail -f,我将无法完全匹配您的使用情况。

我猜你还有其他想要陷阱的条件,1号孩子......等等。只需进行一次reg-exp匹配,以及该案例的单个变量名称,并将其添加到两个地方的&& d测试列表中。

可能有一种方法可以使用awk关联数组进行更多的计算机设计,但这需要额外费用; - )

此脚本可让您了解awk如何成为unix工具包的一部分以及如何将其用于文本处理。

IHTH