-
Notifications
You must be signed in to change notification settings - Fork 19
/
1c_infobases_discovery.ps1
193 lines (149 loc) · 4.67 KB
/
1c_infobases_discovery.ps1
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Param (
[string]$rac,
[string]$ras_port,
[string]$rac_usr,
[string]$rac_pwd
)
# Function is to bring to the format understands zabbix
function convertto-encoding ([string]$from, [string]$to){
begin{
$encfrom = [system.text.encoding]::getencoding($from)
$encto = [system.text.encoding]::getencoding($to)
}
process{
$bytes = $encto.getbytes($_)
$bytes = [system.text.encoding]::convert($encfrom, $encto, $bytes)
$encto.getstring($bytes)
}
}
# Extract value from text line specified to RAC stdout
function get_value_from_text_line([string]$key_and_value) {
$value = ""
$pos = $key_and_value.IndexOf(":")
if ($pos -gt 0) {
$value = $key_and_value.Substring($pos+2, $key_and_value.Length-$pos-2)
$value = $value.Trim(" ")
$value = $value.Trim("`"")
}
return $value
}
# Extract key from text line specified to RAC stdout
function get_key_from_text_line([string]$key_and_value) {
$key = ""
$pos = $key_and_value.IndexOf(":")
if ($pos -gt 0) {
$key = $key_and_value.Substring(0, $pos)
$key = $key.Trim("`"")
}
return $key
}
function get_clusters_info()
{
$data_array = @()
try
{
$stdout_heap = & $rac localhost:$ras_port cluster list 2>&1
if (!$?)
{
write-host $($stdout_heap | convertto-encoding "cp866" "utf-8")
exit;
}
}
catch
{
write-host $($_.Exception.Message | convertto-encoding "cp866" "utf-8")
exit;
}
$cluster_id_heap = $stdout_heap | Select-String -Pattern "cluster:*"
$cluster_name_heap = $stdout_heap | Select-String -Pattern "name:*"
if ($cluster_id_heap.GetType().fullname -eq "Microsoft.PowerShell.Commands.MatchInfo")
{
$cluster_id_heap = [array]$cluster_id_heap
$cluster_name_heap = [array]$cluster_name_heap
}
if ($cluster_name_heap.Count -eq $cluster_name_heap.Count) {
$array_count = $cluster_name_heap.Count
} else {
write-host "Error parsing infobase list"
write-host $($_.Exception)
exit
}
if ($array_count -eq 0)
{
write-host "No clusters"
exit
}
for ($i = 0; $i -lt $array_count; $i++)
{
$description = $(get_value_from_text_line $cluster_id_heap[$i]) + " : " + $(get_value_from_text_line $cluster_name_heap[$i])
$data_array += $description
}
return $data_array
}
function get_infobases_info([string]$cluster_id){
$data_array = @()
try {
if ($rac_usr -eq "") {
$stdout_heap = & $rac localhost:$ras_port infobase summary list --cluster=$cluster_id 2>&1
} else {
$stdout_heap = & $rac localhost:$ras_port infobase summary list --cluster=$cluster_id --cluster-user=$rac_usr --cluster-pwd=$rac_pwd 2>&1
}
if (!$?)
{
write-host $($stdout_heap | convertto-encoding "cp866" "utf-8")
exit;
}
}
catch
{
write-host $($_.Exception.Message | convertto-encoding "cp866" "utf-8")
exit
}
$infobase_id_heap = $stdout_heap | Select-String -Pattern "infobase:*"
$infobase_name_heap = $stdout_heap | Select-String -Pattern "name:*"
if ($infobase_id_heap -eq $null) {
return ""
}
if ($infobase_id_heap.GetType().fullname -eq "Microsoft.PowerShell.Commands.MatchInfo")
{
$infobase_id_heap = [array]$infobase_id_heap
$infobase_name_heap = [array]$infobase_name_heap
}
if ($infobase_id_heap.Count -eq $infobase_name_heap.Count) {
$array_count = $infobase_id_heap.Count
} else {
write-host "Error parsing infobase list"
exit
}
for ($i = 0; $i -lt $array_count; $i++) {
$description = $(get_value_from_text_line $infobase_id_heap[$i]) + " : " + $(get_value_from_text_line $infobase_name_heap[$i])
$data_array += $description
}
return $data_array
}
$data_array = get_clusters_info
$line = ""
$line = $line + "{`n"
$line = $line + " `"data`":[`n"
$line = $line + "`n"
$infobase_count = 0
foreach ($cluster_description in $data_array){
$cluster_id = get_key_from_text_line $cluster_description
$cluster_name = get_value_from_text_line $cluster_description
$infobase_array = get_infobases_info $cluster_id
foreach($infobase_description in $infobase_array)
{
$infobase_count = 1
$line = $line + " { `"{#CLUSTERNAME}`" : `"" + $cluster_name + "`", `"{#CLUSTERID}`" : `"" + $cluster_id + "`", `"{#INFOBASENAME}`" : `"" + $(get_value_from_text_line $infobase_description) + "`", `"{#INFOBASEID}`" : `"" + $(get_key_from_text_line $infobase_description) + "`" },`n"
}
}
if ($infobase_count -lt 1){
write-host "No infobases"
exit
}
$line = $line.TrimEnd(",`n")
$line = $line + "`n"
$line = $line + "`n ]"
$line = $line + "`n}"
$line = $line | convertto-encoding "cp866" "utf-8"
write-host $line