Request mongodb from the command line and send the result as an HTTP request to crontab

I want to include simple monitoring in my application, so I need to send an HTTP request containing the number of documents in the mongodb collection from crontab.

Requests are described at http://countersrv.com/ as follows:

curl http://countersrv.com/ID -d value=1

I need to request mongodb from the command line and get the number of documents in the collection. It should be something like db.my_docs.count().

I want to send this number every hour, so you need to add something like this to crontab:

0 * * * * curl http://countersrv.com/ID -d value=...query mongo here...?
+4
source share
2 answers

crontab, , , /etc/cron.hourly, crontab , , ,

/etc/cron.hourly monitor.sh, script

chmod +x /etc/cron.hourly/monitor.sh

script , mongoscript.js:

use yourdb
db.my_docs.count()

script -

#!/bin/bash
mongo mongoscript.js > output.js
curl http://countersrv.com/ID -d value=@output.js
+7

, " ":

 mongo --quiet --eval 'var db = db.getSiblingDB("database"); print( "value=" + db.collection.count() );' | curl -X POST http://countersrv.com/[edit endpoint] -d @-

--quiet , --eval - , .

, .getSiblingDB() use database . .getCollection(), .

print() "/", pipe - curl " " countersrv, . @- stdin.

+7

Source: https://habr.com/ru/post/1545928/


All Articles