Found the following:
https://gist.github.com/cnauroth/7ff52e9f80e7d856ddb3
This works out of the box on my CDH5 namenodes, although I'm not sure if other hadoop distributions will have http: // namenode: 50070 / jmx - if not, I think it can be added by deploying Jolokia .
Example:
curl 'http://namenode1.example.com:50070/jmx?qry=Hadoop:service=NameNode,name=NameNodeStatus' { "beans" : [ { "name" : "Hadoop:service=NameNode,name=NameNodeStatus", "modelerType" : "org.apache.hadoop.hdfs.server.namenode.NameNode", "State" : "active", "NNRole" : "NameNode", "HostAndPort" : "namenode1.example.com:8020", "SecurityEnabled" : true, "LastHATransitionTime" : 1436283324548 } ]
Thus, choosing one HTTP request for each namenode (this should be fast), we can find out which one is active.
It's also worth noting that if you say the WebHDFS REST API for an inactive namenode, you will get 403 Forbidden and the following JSON:
{"RemoteException":{"exception":"StandbyException","javaClassName":"org.apache.hadoop.ipc.StandbyException","message":"Operation category READ is not supported in state standby"}}
source share