Page 1 of 1

MSSQL Get SQL TPL

Posted: Thu Oct 03, 2019 9:07 am
by hudatolah
tpl 1.14 module AssetInfoFromSQL_MSSQL;

metadata
origin := "BlackholeSurfer";
tree_path := 'BlackholeSurfer','Database,'MSSQL';
end metadata

from SearchFunctions import SearchFunctions 1.5;

definitions MSSQLDetailsSerialNumber 2.0
"""
Relational Database Queries for MSSQL
"""
type := sql_discovery;
group := "Microsoft SQL Server";

define getSerialNumberMSSSQL
"Returns a value from MSSQL db"

query := "SELECT value from from table where field = 'VALUE'";
//parameters := %field%;
end define;
end definitions;

pattern DBSerialNumber_MSSQL 1.5
"""
get a value from SQL
"""
metadata
publishers := 'Microsoft';
products := 'SQL Server';
publisher_synonyms := 'MS';
categories := 'Relational Database Management Systems';
urls := 'http://www.microsoft.com/sql/';
end metadata;

overview
tags RDBMS, Database, SQL, MSSQLServer, CUSTOM;
end overview;

triggers
on db := Database created, confirmed where type = "SQL Server Database";
end triggers;

body
db_host := "";
tables := none;
table_details := [];

mssqls := search(in db traverse Detail:Detail:ElementWithDetail:SoftwareInstance);

if size(mssqls) <> 1 or mssqls[0].type <> "Microsoft SQL Server" then
log.info("Having difficulties finding the Microsoft SQL Server SI realted to %db.name%. Stopping this pattern");
stop;
end if;

mssql := mssqls[0];

// Get Host INFO
da := discovery.access();
host := model.host(da);


db_host := host.name;
port := mssql.port;
database := db.instance;
dbname := db.instance;
sn_query_result := none;
endpoint := mssql.endpoint;
qresult := none;

if host and db_host and mssql.port then
sn_query_result := MSSQLDetailsSerialNumber.getSerialNumberMS(endpoint := host,
port := port,
database := database);

elif mssql.port and db_host then
sn_query_result := MSSQLDetailsSerialNumber.getSerialNumberMS(endpoint := db_host,
port := port;
database := database,
dbname := database);
end if;

qresult := sn_query_result[0].value;
db.serial := '%qresult%';
model.addDisplayAttribute (db,"serial");
end body;
end pattern;