Last active
April 25, 2018 11:12
-
-
Save Areso/486060a612457ca33b29e235f6507885 to your computer and use it in GitHub Desktop.
check_sql_server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
##Import-Module FailoverClusters | |
##Get-ClusterResource | |
#r = subprocess.call(["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "Get-ClusterResource", ""]) | |
#print(r) | |
#print("hw"); | |
args = ["C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe", "Get-ClusterResource"] | |
process = subprocess.Popen(args, stdout=subprocess.PIPE) | |
data = process.communicate() | |
lines_num = 0 | |
my_lines = [] | |
my_lines2 = [] | |
my_lines3 = [] | |
for line in data: | |
lines_num = lines_num + 1 | |
#print(line) | |
my_lines.append(line) | |
lines_num2_cnt = len(my_lines[0].split('\n')) | |
my_lines2 = my_lines[0].split('\n') | |
#print("total lines is "+str(lines_num)+"and addit info is "+str(lines_num2_cnt)) | |
how_many = 0 | |
for line in my_lines2: | |
#if (how_many == 0): | |
#print(line) | |
how_many = how_many+1 | |
#lines_count_without_ending = lines_num2 - 3 | |
for x in range (3, lines_num2_cnt - 3): | |
my_lines3.append(my_lines2[x]) | |
#print(my_lines2[x]) | |
services = [] | |
statuses = [] | |
ownergroup = [] | |
resourcetype = [] | |
for line in my_lines3: | |
services.append(line[0:30].rstrip()) | |
statuses.append(line[30:60].strip()) | |
ownergroup.append(line[60:90].strip()) | |
resourcetype.append(line[90:120].strip()) | |
#print(line) | |
output_cnt = len(services) | |
#print("total output lines is "+str(output_cnt)) | |
#for each in resourcetype: | |
#print(each) | |
error_flag = False | |
tmp_bank = 0 | |
non_cluster_errors = [] | |
tmp_cluster = [] | |
errors_array = [] | |
tmp_ownergroup = [] | |
#array_of_lines = 0 | |
non_sqlavgroup_lines = [] | |
for x in range(output_cnt): | |
if (resourcetype[x] == "SQL Server Availability Group"): | |
tmp_ownergroup.append(ownergroup[x]) | |
#print(ownergroup[x]) | |
for y in tmp_ownergroup: | |
ip_addresses = 0 | |
err_ip_addresses = 0 | |
for x in range(output_cnt): | |
if (ownergroup[x]==y): | |
if (resourcetype[x]=="IP Address"): | |
ip_addresses = ip_addresses + 1 | |
#print(statuses[x]) | |
if (statuses[x]=="Offline"): | |
err_ip_addresses = err_ip_addresses+1 | |
if (resourcetype[x]!="IP Address"): | |
if (statuses[x]=="Offline"): | |
errors_array.append(services[x]+" "+ownergroup[x]+" "+resourcetype[x]+" is "+statuses[x]) | |
error_flag = True | |
else: | |
#array_of_lines = array_of_lines+1 | |
non_sqlavgroup_lines.append(x) | |
#print(err_ip_addresses) | |
#print(ip_addresses) | |
if (err_ip_addresses == ip_addresses): | |
errors_array.append("SQL Server Availability Group IP error") | |
for x in non_sqlavgroup_lines: | |
if (statuses[x] == "Offline" and resourcetype[x]!="IP Address"): | |
errors_array.append(services[x]+" "+ownergroup[x]+" "+resourcetype[x]+" is "+statuses[x]) | |
error_flag = True | |
cluster_group_lines = [] | |
cluster_err_ip_addresses = 0 | |
cluster_ip_addresses = 0 | |
for x in non_sqlavgroup_lines: | |
if (ownergroup[x] == "Cluster Group"): | |
cluster_group_lines.append(x) | |
for x in cluster_group_lines: | |
if (statuses[x] == "Offline" and resourcetype[x] != "IP Address"): | |
errors_array.append(services[x]+" "+ownergroup[x]+" "+resourcetype[x]+" is "+statuses[x]) | |
error_flag = True | |
if (resourcetype[x] == "IP Address"): | |
cluster_ip_addresses = cluster_ip_addresses+1 | |
if (statuses[x] == "Offline"): | |
cluster_err_ip_addresses = cluster_err_ip_addresses + 1 | |
if (cluster_err_ip_addresses == cluster_ip_addresses): | |
errors_array.append("Cluster Group IP error") | |
error_flag = True | |
for x in errors_array: | |
print(x) | |
if (error_flag == True): | |
print("MS SQL SERVER ERROR") | |
else: | |
print("MS SQL SERVER OK") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment