Welcome, guest | Sign In | My Account | Store | Cart

A function to detect the presence of Microsoft Cluster Services on a server, and return the proper Windows network name of the server. Useful in scripts targeting Microsoft SQL Servers, where determining the two part Server\Instance naming convention is necessary.

Python, 28 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# clusterdetect() - M.Keranen (mksql@yahoo.com) [11/24/2003]
# ----------------------------------------------------------
# Detect the presence of a clustered MS SQL Server virtual instance,
# and return the proper SQL Server network name of the server

from _winreg import *

def clusterdetect(server):
     try: rReg = ConnectRegistry(target,HKEY_LOCAL_MACHINE)
     except: 
          print "Unable to connect to registry on server %s" % (server)
          sys.exit()
     else:
          key = "SOFTWARE\\Microsoft\\Microsoft SQL Server\\" + server + "\\Cluster"
          try: rKey = OpenKey(rReg, key)
          except: clustername = server
          else:
               try: clustername = QueryValueEx(rKey,"ClusterName")[0]
               except: 
                    clustername = string.lower(server)
                    print "* No ClusterName key defined in registry on server %s" % (server)
               else: 
                    clustername = string.lower("%s\\%s" % (server,clustername))
                    print "Cluster detected: %s" % (clustername)
                    CloseKey(rKey)
          CloseKey(rReg)

          return clustername