简体   繁体   English

检查状态代码和内容的 wget 响应

[英]Check wget response for both status code and content

I am writing a healthcheck script for docker container using bash.我正在使用 bash 为 docker 容器编写运行状况检查脚本。 It's supposed to check both URL returning status 200 and its content.它应该检查 URL 返回状态 200 及其内容。

Is there a way to use wget to check that URL returns 200 AND that URLs content contains a certain word (in my case that word is "DB_OK") while preferably calling it only once?有没有办法使用 wget 来检查 URL 返回 200 并且 URL 内容包含某个单词(在我的情况下该单词是“DB_OK”),同时最好只调用一次?

StatusString=$(wget -qO- localhost:1337/status)

#does not work
function available_check(){
  if [[ $StatusString != *"HTTP/1.1 200"* ]];
      then AvailableCheck=1
      echo "availability check failed"
  fi
}

#checks that statusString contains "DB_OK", works
function db_check(){
  if [[ $StatusString != *"DB_OK"* ]];
      then DBCheck=1
      echo "db check failed"
  fi
}
#!/bin/bash

# -q removed, 2>&1 added to get header and content
StatusString=$(wget -O- 'localhost:1337/status' 2>&1)

function available_check{
  if [[ $StatusString == *"HTTP request sent, awaiting response... 200 OK"* ]]; then
    echo "availability check okay"
  else
    echo "availability check failed"
    return 1
  fi
}

function db_check{
  if [[ $StatusString == *"DB_OK"* ]]; then
    echo "content check okay"
  else
    echo "content check failed"
    return 1
  fi
}

if available_check && db_check; then
  echo "everything okay"
else
  echo "something failed"
fi

Output:输出:

availability check okay
content check okay
everything okay

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM