Class: HelperFunctions
- Inherits:
-
Object
- Object
- HelperFunctions
- Includes:
- ERB::Util
- Defined in:
- lib/dashing_api/helperFunctions.rb
Instance Method Summary collapse
- #addTile(dashboard, body, directory) ⇒ Object
- #checkArray(body) ⇒ Object
-
#checkAuthToken(body, token) ⇒ Object
Check if the auth_token is a valid.
- #createDashboard(body, dashboard, directory) ⇒ Object
-
#dashboardExists(dashboardName, directory) ⇒ Object
Check if the dashboard exists within the dashboard folder.
- #deleteTile(dashboard, hosts, directory) ⇒ Object
-
#getHost ⇒ Object
Get the hostname.
- #getHtmlElements(dashboard, hosts) ⇒ Object
-
#initialize ⇒ HelperFunctions
constructor
A new instance of HelperFunctions.
- #pingHosts(dashboard, body, directory) ⇒ Object
- #render(body, template) ⇒ Object
- #save(file, body, template) ⇒ Object
-
#tileExists(dashboard, hosts, directory) ⇒ Object
Check if a nagios host/hosts exists on a dashboard.
- #uniq?(array) ⇒ Boolean
- #up?(host) ⇒ Boolean
Constructor Details
#initialize ⇒ HelperFunctions
Returns a new instance of HelperFunctions.
56 57 58 59 |
# File 'lib/dashing_api/helperFunctions.rb', line 56 def initialize @newDashboardTemplate = newDashboardTemplate @addDeleteTileTemplate = addDeleteTileTemplate end |
Instance Method Details
#addTile(dashboard, body, directory) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/dashing_api/helperFunctions.rb', line 169 def addTile(dashboard, body, directory) dashboard = directory+'/dashboards/'+dashboard+'.erb' finalElement = Array.new if checkArray(body) finalElement = getHtmlElements(dashboard, body['tiles']['hosts']) for i in 0..body["tiles"]["hosts"].length-1 host = body["tiles"]["hosts"][i] = body["tiles"]["widgets"][i] title = body["tiles"]["titles"][i] url = body["tiles"]["urls"][i] tileElement = ["<li data-row=\"1\" data-col=\"1\" data-sizex=\"2\" data-sizey=\"2\" onClick=\"myFunction()\"> <div data-id=\"", host,"\" data-view=\"", ,"\" data-unordered=\"true\" data-title=\"", title,"\" data-url=\"", url, "\" data-bind-style=\"status\" style=\"style\"></div> </li>" ].join finalElement.push(tileElement) end save(dashboard, finalElement, @addDeleteTileTemplate) return true else return false end end |
#checkArray(body) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/dashing_api/helperFunctions.rb', line 111 def checkArray(body) hostLen = body["tiles"]["hosts"].length = body["tiles"]["widgets"].length titleLen = body["tiles"]["titles"].length if hostLen.equal?() and .equal?(titleLen) return true else return false end end |
#checkAuthToken(body, token) ⇒ Object
Check if the auth_token is a valid
62 63 64 65 66 67 68 69 |
# File 'lib/dashing_api/helperFunctions.rb', line 62 def checkAuthToken(body, token) auth_token = body["auth_token"] if token == auth_token return true else return false end end |
#createDashboard(body, dashboard, directory) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/dashing_api/helperFunctions.rb', line 123 def createDashboard(body, dashboard, directory) dashboard = directory+'/dashboards/'+dashboard+'.erb' if checkArray(body) save(dashboard, body, @newDashboardTemplate) return true else return false end end |
#dashboardExists(dashboardName, directory) ⇒ Object
Check if the dashboard exists within the dashboard folder
92 93 94 95 96 97 98 |
# File 'lib/dashing_api/helperFunctions.rb', line 92 def dashboardExists(dashboardName, directory) if File.exist?(directory+'/dashboards/'+dashboardName+'.erb') return true else return false end end |
#deleteTile(dashboard, hosts, directory) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/dashing_api/helperFunctions.rb', line 150 def deleteTile(dashboard, hosts, directory) liElements = Array.new finalElement = Array.new dashboard = directory+'/dashboards/'+dashboard+'.erb' doc = Nokogiri::HTML(open(dashboard)) liElements = doc.search('div > ul > li') liElements.each do |item| element = item.to_s if hosts.any? { |w| element[w] } next else finalElement.push(element) end end save(dashboard, finalElement, @addDeleteTileTemplate) end |
#getHost ⇒ Object
Get the hostname
266 267 268 |
# File 'lib/dashing_api/helperFunctions.rb', line 266 def getHost() return Socket.gethostname end |
#getHtmlElements(dashboard, hosts) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/dashing_api/helperFunctions.rb', line 133 def getHtmlElements(dashboard, hosts) liElements = Array.new finalElement = Array.new doc = Nokogiri::HTML(open(dashboard)) liElements = doc.search('div > ul > li') liElements.each do |item| element = item.to_s if hosts.any? { |w| element[w] } next else finalElement.push(element) end end return finalElement end |
#pingHosts(dashboard, body, directory) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/dashing_api/helperFunctions.rb', line 207 def pingHosts(dashboard, body, directory) upArr = Array.new downArr = Array.new if uniq?(body["tiles"]["hosts"]) body["tiles"]["hosts"].each do |host| if up?(host) upArr.push(host) else downArr.push(host) end end upOutput = tileExists(dashboard, upArr, directory) if !upOutput.empty? hosts = Array.new titles = Array.new = Array.new urls = Array.new for i in 0..upOutput.length-1 host = upOutput[i] for j in 0..body["tiles"]["hosts"].length-1 checkhost = body["tiles"]["hosts"][j] if checkhost == host hosts.push(host) .push(body["tiles"]["widgets"][j]) titles.push(body["tiles"]["titles"][j]) urls.push(body["tiles"]["urls"][j]) else next end end end jsonArray = { :tiles => { :hosts => hosts, :widgets => , :titles => titles, :urls => urls} }.to_json objArray = JSON.parse(jsonArray) addTile(dashboard, objArray, directory) end downOutput = tileExists(dashboard, downArr, directory) if !downOutput.empty? tileToRemove = downArr - downOutput else tileToRemove = downArr end deleteTile(dashboard, tileToRemove, directory) return true else return false end end |
#render(body, template) ⇒ Object
100 101 102 |
# File 'lib/dashing_api/helperFunctions.rb', line 100 def render(body, template) ERB.new(template).result(binding) end |
#save(file, body, template) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/dashing_api/helperFunctions.rb', line 104 def save(file, body, template) File.new(file, "w") File.open(file, "w+") do |f| f.write(render(body, template)) end end |
#tileExists(dashboard, hosts, directory) ⇒ Object
Check if a nagios host/hosts exists on a dashboard
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/dashing_api/helperFunctions.rb', line 72 def tileExists(dashboard, hosts, directory) if hosts.kind_of?(Array) arrHosts = hosts else arrHosts = hosts.split(",") end doNotExist = Array.new for hosts in arrHosts host = "data-id=\""+hosts+"\"" if File.foreach(directory+'/dashboards/'+dashboard+'.erb').any?{ |l| l[host] } next else doNotExist.push(hosts) end end return doNotExist end |
#uniq?(array) ⇒ Boolean
199 200 201 202 203 204 205 |
# File 'lib/dashing_api/helperFunctions.rb', line 199 def uniq?(array) if array.length == array.uniq.length return true else return false end end |
#up?(host) ⇒ Boolean
193 194 195 196 |
# File 'lib/dashing_api/helperFunctions.rb', line 193 def up?(host) check = Net::Ping::External.new(host) return check.ping? end |