Skip to content

Commit

Permalink
Up to the stars
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Rataj committed Jun 29, 2021
0 parents commit 88c2747
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 0 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Create a container

on:
push:
# Publish `v1.2.3` tags as releases.
tags:
- v*
jobs:

# Push image to GitHub Packages.
# See also https://docs.docker.com/docker-hub/builds/
push:
runs-on: ubuntu-latest
if: github.event_name == 'push'

steps:
- uses: actions/checkout@v2

- name: Build image
run: docker build . --file Dockerfile --tag image

- name: Log into registry
run: echo "${{ secrets.CR_PAT }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Push image
run: |
IMAGE_ID=ghcr.io/${{ github.repository }}
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Strip "v" prefix from tag name
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
# keep only the major.minor release, no patch
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/\.[0-9]*$//')
echo IMAGE_ID=$IMAGE_ID
echo VERSION=$VERSION
docker tag image $IMAGE_ID:$VERSION
docker push $IMAGE_ID:$VERSION
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.env
53 changes: 53 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
FROM php:7.4.10-alpine3.12
LABEL maintainer="Daniel Rataj <daniel.rataj@centrum.cz>"
LABEL org.opencontainers.image.source="https://github.com/whoopsmonitor/whoopsmonitor-check-rabbitmq-queue-count"
LABEL com.whoopsmonitor.documentation="https://github.com/whoopsmonitor/whoopsmonitor-check-rabbitmq-queue-count"
LABEL com.whoopsmonitor.env.WM_RABBITMQ_QUEUE=""
LABEL com.whoopsmonitor.env.WM_RABBITMQ_HOST=""
LABEL com.whoopsmonitor.env.WM_RABBITMQ_PORT=""
LABEL com.whoopsmonitor.env.WM_RABBITMQ_LOGIN=""
LABEL com.whoopsmonitor.env.WM_RABBITMQ_PASSWORD=""
LABEL com.whoopsmonitor.env.WM_RABBITMQ_VHOST="/"
LABEL com.whoopsmonitor.env.WM_IS_PASSIVE="true, false or delete this line"
LABEL com.whoopsmonitor.env.WM_IS_DURABLE="true, false or delete this line"
LABEL com.whoopsmonitor.env.WM_IS_EXLUSIVE="true, false or delete this line"
LABEL com.whoopsmonitor.env.WM_IS_AUTO_DELETE="true, false or delete this line"
LABEL com.whoopsmonitor.env.WM_IS_IS_NOWAIT="true, false or delete this line"
LABEL com.whoopsmonitor.env.WM_THRESHOLD_WARNING="10"
LABEL com.whoopsmonitor.env.WM_THRESHOLD_CRITICAL="20"

SHELL ["/bin/sh", "-o", "pipefail", "-c"]

RUN apk add \
# --repository http://dl-cdn.alpinelinux.org/alpine/v3.6/main \
--no-cache \
aspell-dev=0.60.8-r0 \
autoconf=2.69-r2 \
build-base=0.5-r2 \
linux-headers=5.4.5-r1 \
libaio-dev=0.3.112-r1 \
rabbitmq-c-dev=0.10.0-r1 \
&& pecl install amqp \
&& docker-php-ext-enable amqp \
&& docker-php-ext-install sockets \
&& rm -rf /var/cache/apk/*

# installl composer
ARG COMPOSER_VERSION=1.8.5
ARG COMPOSER_SHA256=4e4c1cd74b54a26618699f3190e6f5fc63bb308b13fa660f71f2a2df047c0e17
# hadolint ignore=SC2046
RUN curl -Ls "https://getcomposer.org/download/${COMPOSER_VERSION}/composer.phar" > /usr/local/bin/composer \
&& test $(sha256sum /usr/local/bin/composer | cut -d ' ' -f 1) = ${COMPOSER_SHA256} \
&& chmod +x /usr/local/bin/composer

ARG COMPOSER_ALLOW_SUPERUSER=1

COPY ./src/composer.json .

RUN COMPOSER_CACHE_DIR=/tmp/composer_cache composer install --no-ansi --no-interaction --no-autoloader

COPY . .

RUN composer dump-autoload --no-ansi --no-interaction --optimize

CMD [ "php", "./src/index.php"]
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# whoopsmonitor-check-rabbitmq-queue-count
Check amount of records in RabbitMQ queue.

## Environmental variables

- `WM_RABBITMQ_QUEUE`
- `WM_RABBITMQ_HOST`
- `WM_RABBITMQ_PORT`
- `WM_RABBITMQ_LOGIN`
- `WM_RABBITMQ_PASSWORD`
- `WM_RABBITMQ_VHOST`
- `WM_IS_PASSIVE`
- `WM_IS_DURABLE`
- `WM_IS_EXLUSIVE`
- `WM_IS_AUTO_DELETE`
- `WM_IS_NOWAIT`
- `WM_THRESHOLD_WARNING`, default is 10
- `WM_THRESHOLD_CRITICAL`, default is 20

### `RABBITMQ_QUEUE`
You can use multiple queues. Just separate then with coma like:

```yaml
WM_RABBITMQ_QUEUE=my-queue,my-other-queue
```

### Thresholds
You can override either warning or critical thresholds. You can set the threshold right after the check name, separated with colon. First one is for warning and the second one for critical level.

Example:

```yaml
WM_RABBITMQ_QUEUE=my-queue:50:100
```

## Example

Details of the check in Whoops Monitor configuration tab or for the `.env` file.

```yaml
WM_RABBITMQ_QUEUE=my-queue
WM_RABBITMQ_HOST=localhost
WM_RABBITMQ_PORT=5672
WM_RABBITMQ_LOGIN=user
WM_RABBITMQ_PASSWORD=password
WM_RABBITMQ_VHOST=/
WM_IS_PASSIVE=false
WM_IS_DURABLE=false
WM_IS_EXLUSIVE=false
WM_IS_AUTO_DELETE=false
WM_IS_NOWAIT=false
WM_THRESHOLD_WARNING=30
WM_THRESHOLD_CRITICAL=80
```

## Output

- `0` - Amount of records is ok.
- `1` - Amount of records is at warning level.
- `2` - Amount of records is at critical level.


## Build
```bash
docker build -t whoopsmonitor-check-rabbitmq-queue-count .
```

## Run

```bash
docker run --rm --env-file .env whoopsmonitor-check-rabbitmq-queue-count
```
5 changes: 5 additions & 0 deletions src/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"php-amqplib/php-amqplib": "~2.12.0"
}
}
108 changes: 108 additions & 0 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

use PhpAmqpLib\Connection\AMQPStreamConnection;

$THRESHOLD_WARNING = getenv('WM_THRESHOLD_WARNING');
$THRESHOLD_CRITICAL = getenv('WM_THRESHOLD_CRITICAL');

if (!$THRESHOLD_WARNING) {
$THRESHOLD_WARNING = 10;
}

if (!$THRESHOLD_CRITICAL) {
$THRESHOLD_CRITICAL = 20;
}

$autoloadFile = dirname(dirname(__FILE__)) . '/vendor/autoload.php';

if (!file_exists($autoloadFile) || !is_readable($autoloadFile)) {
echo sprintf('CRITICAL: autoload.php file does not exists in path %s', $autoloadFile);
exit(2);
}

require_once $autoloadFile;

$queues = explode(',', getenv('WM_RABBITMQ_QUEUE'));

$resultsOk = [];
$resultsWarning = [];
$resultsCritical = [];


try {
$connection = new AMQPStreamConnection(
getenv('WM_RABBITMQ_HOST'),
getenv('WM_RABBITMQ_PORT'),
getenv('WM_RABBITMQ_LOGIN'),
getenv('WM_RABBITMQ_PASSWORD'),
getenv('WM_RABBITMQ_VHOST')
);

foreach ($queues as $queue) {
$channel = $connection->channel();
// explode tresholds
list($queueName, $levelWarning, $levelCritical) = explode(':', $queue);

if (!$levelWarning) $levelWarning = $THRESHOLD_WARNING;
if (!$levelCritical) $levelCritical = $THRESHOLD_CRITICAL;

$output = $channel->queue_declare($queueName,
getenv('WM_IS_PASSIVE') ?: false,
getenv('WM_IS_DURABLE') ?: false,
getenv('WM_IS_EXLUSIVE') ?: false,
getenv('WM_IS_AUTO_DELETE') ?: false,
getenv('WM_IS_NOWAIT') ?: false
);

$numberOfMessagesInQueue = (int)(isset($output[1]) ? $output[1] : 0);
$channel->close();

if ($numberOfMessagesInQueue > $levelCritical) {
$resultsCritical[] = sprintf('[%s][%s] Too many records: %d', '!!!', $queueName, $numberOfMessagesInQueue);
continue;
}

if ($numberOfMessagesInQueue <= $levelCritical && $numberOfMessagesInQueue > $levelWarning) {
$resultsWarning[] = sprintf('[%s][%s] Too many records: %d', '!', $queueName, $numberOfMessagesInQueue);
continue;
}

$resultsOk[] = sprintf('[%s][%s] ok: %d', '', $queueName, $numberOfMessagesInQueue);
}
} catch (Exception $e) {
echo sprintf('CRITICAL: There is some error in Rabbit connection: %s', $e->getMessage());
exit(2);
}

$connection->close();

if ($resultsCritical) {
echo implode(PHP_EOL, $resultsCritical);
echo PHP_EOL;
}

if ($resultsWarning) {
echo implode(PHP_EOL, $resultsWarning);
echo PHP_EOL;
}

if ($resultsOk) {
echo implode(PHP_EOL, $resultsOk);
echo PHP_EOL;
}

// now exit with a proper code
if ($resultsCritical) {
exit(2);
}

if ($resultsWarning) {
exit(2);
}

if ($resultsOk) {
exit(0);
}

echo 'Some uknown error appeared';
exit(2);

0 comments on commit 88c2747

Please sign in to comment.