[ SEA-GHOST MINI SHELL]

Path : /proc/2/cwd/proc/2/cwd/proc/2/cwd/proc/2/root/var/lib/zabbix/
FILE UPLOADER :
Current File : //proc/2/cwd/proc/2/cwd/proc/2/cwd/proc/2/root/var/lib/zabbix/get_mssql.py

#!/usr/bin/python3

import pymssql
import json
import sys
import datetime

#DBHOST = "10.21.24.167"
#DBUSER = "INTERNAL\monitoring-ldap-read"
#DBPASS = "asd@#4sd3S#s3S!d56"
#DBNAME = "master"
#DB = "SUSDB"
#TODO = "base_in_cluster_replica_state"

DBHOST = sys.argv[1]
DBUSER = sys.argv[2]
DBPASS = sys.argv[3]
DB = sys.argv[4]
TODO = sys.argv[5]

conn = pymssql.connect(server=DBHOST, user=DBUSER, password=DBPASS)
cursor = conn.cursor()

if TODO == "clusterstate":
    cursor.execute("""
SELECT ag.name as group_name,
ISNULL(ags.primary_recovery_health, 2) as primary_recovery_health,
ISNULL(ags.primary_replica, 'Unknown') as primary_replica,
ISNULL(ags.secondary_recovery_health, 2) as secondary_recovery_health,
ags.synchronization_health as synchronization_health
FROM sys.dm_hadr_availability_group_states ags JOIN sys.availability_groups ag ON ag.group_id = ags.group_id
FOR JSON AUTO;
""")
    rows = cursor.fetchall()
    print(rows)
elif TODO == "state":
    cursor.execute(f"""
        SELECT state FROM sys.databases where  name = '{DB}'
        """)
    rows = cursor.fetchall()
    print(rows)
elif TODO == "base_in_cluster_replica_state":
    cursor.execute(f"""
SELECT
dbcs.database_name AS [DatabaseName],
ISNULL(dbrs.synchronization_state, 0) AS [SynchronizationState],
ISNULL(dbrs.is_suspended, 0) AS [IsSuspended],
ISNULL(dbcs.is_database_joined, 0) AS [IsJoined]
FROM master.sys.availability_groups AS AG
LEFT OUTER JOIN master.sys.dm_hadr_availability_group_states as agstates
   ON AG.group_id = agstates.group_id
INNER JOIN master.sys.availability_replicas AS AR
   ON AG.group_id = AR.group_id
INNER JOIN master.sys.dm_hadr_availability_replica_states AS arstates
   ON AR.replica_id = arstates.replica_id AND arstates.is_local = 1
INNER JOIN master.sys.dm_hadr_database_replica_cluster_states AS dbcs
   ON arstates.replica_id = dbcs.replica_id
LEFT OUTER JOIN master.sys.dm_hadr_database_replica_states AS dbrs
   ON dbcs.replica_id = dbrs.replica_id AND dbcs.group_database_id = dbrs.group_database_id
ORDER BY AG.name ASC, dbcs.database_name
FOR JSON AUTO;
            """)
    rows = cursor.fetchall()
    print(rows)
#
# State of replication database
elif TODO == "replication_state":
    cursor.execute(f"""
Select SynchronizationState from
(SELECT
dbcs.database_name AS [DatabaseName],
ISNULL(dbrs.synchronization_state, 0) AS [SynchronizationState],
ISNULL(dbrs.is_suspended, 0) AS [IsSuspended],
ISNULL(dbcs.is_database_joined, 0) AS [IsJoined]
FROM master.sys.availability_groups AS AG 
LEFT OUTER JOIN master.sys.dm_hadr_availability_group_states as agstates
   ON AG.group_id = agstates.group_id
INNER JOIN master.sys.availability_replicas AS AR
   ON AG.group_id = AR.group_id
INNER JOIN master.sys.dm_hadr_availability_replica_states AS arstates
   ON AR.replica_id = arstates.replica_id AND arstates.is_local = 1
INNER JOIN master.sys.dm_hadr_database_replica_cluster_states AS dbcs
   ON arstates.replica_id = dbcs.replica_id
LEFT OUTER JOIN master.sys.dm_hadr_database_replica_states AS dbrs
   ON dbcs.replica_id = dbrs.replica_id AND dbcs.group_database_id = dbrs.group_database_id
   WHERE dbcs.database_name = '{DB}' ) AS SUBQUERY
        """)
    rows = cursor.fetchall()
    print(rows)

# Role of node  LocalReplicaRole (1=primary,2=secondary,3=none)
elif TODO == "roleofnode":
    cursor.execute("""
SELECT LocalReplicaRole from
(SELECT
AG.name AS [Name],
ISNULL(agstates.primary_replica, '') AS [PrimaryReplicaServerName],
ISNULL(arstates.role, 3) AS [LocalReplicaRole]
FROM master.sys.availability_groups AS AG
LEFT OUTER JOIN master.sys.dm_hadr_availability_group_states as agstates
    ON AG.group_id = agstates.group_id
INNER JOIN master.sys.availability_replicas AS AR
    ON AG.group_id = AR.group_id
INNER JOIN master.sys.dm_hadr_availability_replica_states AS arstates
    ON AR.replica_id = arstates.replica_id AND arstates.is_local = 1
) AS SUBQUERY
        """)
    rows = cursor.fetchall()
    print(rows)

else:
    print("ZBX UNSUPPORTED")


SEA-GHOST - SHELL CODING BY SEA-GHOST