Marklogic extract DB status from multiple servers? - python

I need to create a bash(or python) script which gives me the availability status of multiple databases which are on different servers. I found that I can get the status using this url "http://marklogic:8002/manage/v2/database/$DBNAME/?view=status". But I have for about twenty different DBs. When you open this link it generates an xml with database details. Can you please advise how can I loop all the links and grep only the status row ? Or if you have any other idea please advise

It might be worth looking into the MarkLogic Python API project on Github:
https://github.com/marklogic/python_api
HTH!

You can keep the dbnames in a file and then use for loop around it.
for a in `cat dbname.txt`
do
status = `wget -qO- "http://marklogic:8002/manage/v2/database/${a}/?view=status"`
echo $a, $status
done

yep, I did it through curl --anyauth --user user:pass "http://marklogic:8002/manage/v2/database/${a}/?view=status

Related

How to retrieve CouchDB replication status from Python

In bash I can retrieve a json blob of CouchDB replication status docs easily:
curl -X GET -u admin:password localhost:5984/_scheduler/docs/_replicator
Is it possible to retrieve the same information in Python using the couchdb library? I've tried this:
couch_db.view("_scheduler/docs/_replicator", include_docs=True)
.. but this returns a ('not_found', 'missing') error.
It turns out that I was making this more complicated than required. Using the Python requests library makes this task trivial:
requests.get("localhost:5984/_scheduler/docs/_replicator", auth=("admin", "password"))

Zabbix Action - How to use Default Field in Custom Script

I am making some script in python which is run by Zabbix Action.
I want to add value in
Default subject and Default message in Action fields and then use this values in my script. So I am running script and forward all needed macros in script parameters like:
python /path/script.py -A "{HOST.NAME}" -B "{ALERT.MESSAGE}" -C "{ALERT.SUBJECT}"
and i can get only HOST.NAME value, for others I get only macros name but no value
Have you any idea where is the problem? Those macros are unavailable using by Custom scripts?
example
After doing some research & testing myself, it seems as if these Alert macros are indeed not available in a custom script operation.1
You have two options for a workaround:
If you need to be able to execute this script on the host itself, the quick option is to simply replace the macro with the actual text of your subject & alert names. Some testing is definitely necessary to make sure it will work with your environment, and it's not the most elegant solution, but something like this may well work with little extra effort:
python /path/script.py -A "{HOST.NAME}" -B "Problem: {EVENT.NAME}" -C "Problem started at {EVENT.TIME} on {EVENT.DATE}
Problem name: {EVENT.NAME}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
{TRIGGER.URL}"
Verifying of course that e.g. the newlines do not break your custom script in your environment.
It doesn't look pretty but it may well be the easiest option.
If you can run the command on any host, the nicer option is to create a new Media type, which will let you use these variables and may even make adding this script to other hosts much easier. These macros can definitely be used as part of a custom Media type (see Zabbix Documentation - Media Types) which can include custom scripts.
You'll need to make a bash or similar script file for the Zabbix server to run (which means doing anything on a host outside the Zabbix server itself is going to be more difficult, but not impossible).
Once the media type is setup, as a bit of a workaround (not ideal, of course) you'll need a user to 'send' to; assigning that media type to the user and then 'sending' the alert to the user with that media type should execute your script with the macros just like executing the custom command.
1: While I did do my own testing on this, I couldn't found any documentation which specifically states that these macros aren't supported in this case, and they definitely look like they should be - more than happy to edit/revoke this answer if anyone can find documentation that confirms or denies this.
I should also explain how it works now, so I did sth like:
python /path/script.py -A "{HOST.NAME}" -B "Problem: {EVENT.NAME}" -C "Problem started at {EVENT.TIME} on {EVENT.DATE}
Problem name: {EVENT.NAME}
Host: {HOST.NAME}
Severity: {EVENT.SEVERITY}
Original problem ID: {EVENT.ID}
{TRIGGER.URL}"
works for me :)

Sending messages with Telegram - APIs or CLI?

I would like to be able to send a message to a group chat in Telegram. I want to run a python script (which makes some operations that already works) and then, if some parameters have some values the script should send a message to a group chat through Telegram. I am using Ubuntu, and Python 2.7
I think, if I am not wrong, that I have two ways to do that:
Way One: make the Python script connect to the Telegram APIs directly and send the message (https://core.telegram.org/api).
Way Two: make the Python script call the Telegram's CLI (https://github.com/vysheng/tg), pass some values to this and then the message is sent by the Telegram's CLI.
I think that the first way is longer, so a good idea might be using the Way Two.
In this case I really don't know how to proceed.
I don't know lots about scripts in linux, but I tried to do this:
#!/bin/bash
cd /home/username/tg
echo "msg user#******** messagehere" | ./telegram
sleep 10
echo "quit" | ./telegram
this works at a half: it sends the message correctly, but then the process remains open. And second problem, I have no clue on how to call that from python and how to pass some value to this script. The value that I would like to pass to the script is the "messagehere" var: this would be a 100/200 characters message, defined from inside the python script.
Does anyone has any clues on that?
Thanks for replies, I hope this might be useful for someone else.
Telegram recently released their new Bot API which makes sending/receiving messages trivial. I suggest you also take a look at that and see if it fits your needs, it beats wrapping the client library or integrating with their MTProto API.
import urllib
import urllib2
# Generate a bot ID here: https://core.telegram.org/bots#botfather
bot_id = "{YOUR_BOT_ID}"
# Request latest messages
result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/getUpdates").read()
print result
# Send a message to a chat room (chat room ID retrieved from getUpdates)
result = urllib2.urlopen("https://api.telegram.org/bot" + bot_id + "/sendMessage", urllib.urlencode({ "chat_id": 0, "text": 'my message' })).read()
print result
Unfortunately I haven't seen any Python libraries you can interact directly with, but here is a NodeJS equivalent I worked on for reference.
Since version 1.05 you can use the -P option to accept messages from a socket, which is a third option to solve your problem. Sorry that it is not really the answer to your question, but I am not able to comment your question because I do not have enough reputation.
First create a bash script for telegram called tg.sh:
#!/bin/bash
now=$(date)
to=$1
subject=$2
body=$3
tgpath=/home/youruser/tg
LOGFILE="/home/youruser/tg.log"
cd ${tgpath}
${tgpath}/telegram -k ${tgpath}/tg-server.pub -W <<EOF
msg $to $subject
safe_quit
EOF
echo "$now Recipient=$to Message=$subject" >> ${LOGFILE}
echo "Finished" >> ${LOGFILE}
Then put the script in the same folder than your python script, and give it +x permission with chmod +x tg.sh
And finally from python, you can do:
import subprocess
subprocess.call(["./tg.sh", "user#****", "message here"])
I'm working with pytg which could be found here:
A Python package that wraps around Telegram messenger CLI
it works pretty good. I already have a python bot based on that project
You can use safe_quit to terminate the connection instead since it waits until everything is done before closing the connection and termination the application
#!/bin/bash
cd /home/username/tg
echo "msg user#******** messagehere\nsafe_quit\n" | ./telegram
use this as a simple script and call it from python code as the other answer suggested.
I would recommend the first option.
Once you are comfortable with generating an AuthKey, you should start to get a handle on the documentation.
To help, I have written a detailed step-by step guide of how I wrote the AuthKey generation code from scratch here.
It's in vb.net, but the steps should help you do same in python.

How to block aceess to a HTTPS site using python-iptables

I want to implement a simple firewall using python which will block the access to a given list of sites. To do this I want to know how to block a specific site using python-iptables. For example how to block the access to www.facebook.com?
Try this link, it works perfect, use subprocess and say block.
or you can also use iptables as well
iptables -A OUTPUT -d www.facebook.com -j DROP

Filling Sql table with a script

I have a SQL database and I want to fill tables with a script. The following source code shows what I have done so far:
#!/bin/tcsh
mysql -h dbs1 -D my_devel -u USER --password=XYZ
insert into my_table ( col_id, type, file, result, signature) values (***,'###','+++','$$$','...');
exit
According that I pass the parameters '*','#','+','$' and '.' via a method defined in python.
But somehow this is not working for me. Can someone tell me why, or suggest an alternative?
This seems to work fine for me:
#!/bin/tcsh
# count-rows.csh
mysql -h 127.0.0.1 -D tpcw -u root <<EOF
select count(*) from $1;
exit
EOF
You need to send the commands to execute to the standard input of the mysql utility. There are several ways to do that, but using a here document is probably the easiest method.
That said, if your data comes from a Python program, you should look at using a proper MySQL connector for Python. You should see a definite performance increase, but more importantly you will be able to handle any errors properly.
Expanding on Martijn's comment, one of the easiest ways to connect to a MySQL database is using python-mysqldb.

Categories