Registering a service automatically using curl

806 views
Skip to first unread message

George Chilumbu

unread,
Mar 10, 2016, 10:08:00 PM3/10/16
to Consul


I am trying to register a service but i am getting an error with the curl command. Basically, what i am trying to achieve is that when mysqld is down, i want the service "tag" to change from active to inactive. Hence i have created two services in a script folder called active and inactive respectively. 

Below is the script that performs the health check of the mysqld and tries to register the "inactive "service:

#!/bin/bash

nc -z localhost 3306
rt_val=$?

down="Percona XtraDB Cluster down and stopped running";
up="Percona XtraDB Cluster up and running";
if [ $rt_val != 0 ]; then
   echo $down
   curl -s --upload-file /etc/consul.d/scripts/inactive.json http://127.0.0.1:8500/v1/agent/service/register
    exit 3
else
    echo $up
    exit 0
fi




Below is the "inactive" service at /etc/consul.d/scripts/inactive.json:

{
    "name": "db-pxc-test",
    "tags": [
        "inactive"
    ],
    "port": 3306,
    "checks": [
        {
            "script": "/etc/consul.d/scripts/mysql.sh",
            "interval": "5s"
        }
    ],
    "enableTagOverride": true
}

And here is the main service at /etc/consul.d/services.json

{
    "services": [
        {
            "name": "db-pxc-test",
            "tags": [
                "active"
            ],
            "port": 3306,
            "checks": [
                {
                    "script": "/etc/consul.d/scripts/mysql.sh",
                    "interval": "5s"
                }
            ],
             "enableTagOverride": true
        }
    ]
}



Below is the error i am getting in the web ui



What is wrong with the curl statement that tries to register the inactive.son service? I have done something similar in redis to check if the node is a master or slave and it works just fine without any issues. 

James Phillips

unread,
Mar 10, 2016, 10:59:42 PM3/10/16
to consu...@googlegroups.com
Hi George,

That error looks like it involves a script called mysql.pl - is it possible it's not running the script you expect?

-- James

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/consul/issues
IRC: #consul on Freenode
---
You received this message because you are subscribed to the Google Groups "Consul" group.
To unsubscribe from this group and stop receiving emails from it, send an email to consul-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/consul-tool/a906c9e6-b039-49d4-8445-2200b9b59f69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

George Chilumbu

unread,
Mar 10, 2016, 11:34:29 PM3/10/16
to Consul
When i run the curl statement from the command line, it runs just fine and does what i expect it to do.

But when i run it from the perl script, it produces the error above. That is why i am confused. 

Let me clarify one thing, the mysql.sh script is actually the mysql.pl script. I just manually edited the extension when posting this here. I also just edited the mysql.sh script by using bash, when the original code that was producing this error is actually perl code. So here is the original perl code in mysql.pl that was producing this error. Sorry for creating this confusion.

#!/usr/bin/perl

use 5.010;
use strict;

use DBI;
use IO::File;

INIT {
    my $now = localtime();
    print "$now\n";

    my $fh = IO::File->new('/net/account/admin/george/BK/account.txt') or croak $!;
    my $username = $fh->getline;
    my $password = $fh->getline;
    $fh->close;

    chomp $password;
    chomp $username;

    my $rmt = '10.1.4.101';

   my $dbrmt = DBI->connect("dbi:mysql:database=;host=$rmt", $username, $password, {mysql_connect_timeout => 1});
  

    # CHeck if remote mysql server status
    if (!defined $dbrmt) {
       print "Cannot connect to mysql on db-gc-pxc1.\n";
       curl -X PUT -d --upload-file /etc/consul.d/scripts/active.json http://127.0.0.1:8500/v1/agent/service/register print
        exit -1;
        #curl -s --upload-file /etc/consul.d/scripts/inactive.json http://10.1.5.106:8500/v1/agent/service/register;

    }
    if ( $dbrmt->do("SELECT 1") ) {
        print "Mysql on db-gc-pxc1 is running.\n";
    }
    else {
        print "Select 1 Error.";
        exit -1;
        # execute "SELECT 1" error, return exit -1 for instance
    }
    $dbrmt->disconnect;
}
__END__


So basically, what i want to do is that if the remote mysqld is down, i want to change the tag on the local consul service (originally set to inactive) to "active" status.

So this perl script is actually checking the status of a mysqld on a remote server. When i run the perl script from the command line without the curl line, it runs just fine. But when i add the curl statement, thats when i am getting the reported error.

James Phillips

unread,
Mar 11, 2016, 12:55:00 AM3/11/16
to consu...@googlegroups.com
Ah ok - Perl doesn't understand the curl command because you can't inline commands like you can with a shell script. Something like system() will let you call a shell command like you are trying to do (http://perldoc.perl.org/functions/system.html). Alternatively you could talk to Consul using a Perl HTTP client, or even a Perl Consul client. I found https://github.com/robn/Consul but I haven't used it personally.

-- James

George Chilumbu

unread,
Mar 11, 2016, 1:34:23 AM3/11/16
to Consul
Thanks James. Using system() like you suggested works.

new to perl.

You received this message because you are subscribed to a topic in the Google Groups "Consul" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/consul-tool/lJi5gUGnP_I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to consul-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/consul-tool/CAGoWc05yXFVB%3D%2Bx3gWJpwEtSK2EgCjrqNHuTEypCUFU3NGcX%3DA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages