<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IoT | TekCookie</title>
	<atom:link href="https://tekcookie.com/category/iot/feed/" rel="self" type="application/rss+xml" />
	<link>https://tekcookie.com</link>
	<description>Everything about IT</description>
	<lastBuildDate>Sun, 13 Jun 2021 14:02:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://i0.wp.com/tekcookie.com/wp-content/uploads/2021/06/cropped-TekCookie-211.png?fit=32%2C17&#038;ssl=1</url>
	<title>IoT | TekCookie</title>
	<link>https://tekcookie.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">174510684</site>	<item>
		<title>Automate Data Centre environment monitoring using PowerShell</title>
		<link>https://tekcookie.com/monitor-temperature-humidity-sensors-using-powershell/</link>
					<comments>https://tekcookie.com/monitor-temperature-humidity-sensors-using-powershell/#respond</comments>
		
		<dc:creator><![CDATA[jeffythampi]]></dc:creator>
		<pubDate>Wed, 17 Jul 2019 07:07:29 +0000</pubDate>
				<category><![CDATA[Data Centre]]></category>
		<category><![CDATA[IoT]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Automate and Consolidate Temperature Sensors Monitoring Using PowerShell]]></category>
		<category><![CDATA[Automate Data Centre environment monitoring using PowerShell]]></category>
		<category><![CDATA[automating Temperature Sensor monitoring]]></category>
		<category><![CDATA[Data Centre environment monitoring]]></category>
		<category><![CDATA[microgoose ITwatchdog monitor]]></category>
		<category><![CDATA[Monitor temperature and humidity sensor]]></category>
		<guid isPermaLink="false">https://adminscripter.wordpress.com/?p=149</guid>

					<description><![CDATA[In many SME organizations, tracking the indoor climate monitor devices becomes difficult when the number of devices increases. These devices supports multiple interfaces such as web, SNMP, e-mail etc. Integrating SNMP with network monitor application provides a central platform for reporting. But without network monitoring software, it is difficult to monitor these devices. One other [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>In many SME organizations, tracking the indoor climate monitor devices becomes difficult when the number of devices increases. </p>



<p>These devices supports multiple interfaces such as web, SNMP, e-mail etc. Integrating SNMP with network monitor application provides a central platform for reporting. But without network monitoring software, it is difficult to monitor these devices.</p>



<p>One other way is to use powershell to collect the information from the web interface of these devices.</p>



<h3 class="wp-block-heading">Use Case:  Automate and Consolidate Temperature Sensors Monitoring Using PowerShell</h3>



<div class="wp-block-image"><figure class="aligncenter size-large is-resized"><img data-recalc-dims="1" fetchpriority="high" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2020/04/thermometer-1917500_640-1.png?resize=440%2C169&#038;ssl=1" alt="" class="wp-image-1075" width="440" height="169"/></figure></div>



<p><strong>Mail from CIO:</strong> There has been an incident where the 
AC failed and temperature sensors started firing alerts. Luckily, the 
team was able to solve the problem except for one which went unnoticed 
as the mailbox was bombarded with the alerts. We need to have a 
consolidated view and alert mechanism of temperature/humidity monitoring
 sensors located across different location. Lets work on this.</p>



<p><strong>Solution</strong>: The temperature sensors has SNMP builtin  with which we can configure in any monitoring software, but it needs  licenses and there comes the factor of cost. An alternative way is to  use Powershell and query the temperature/humidity information from the  web interface or Xml response (if the device supports). For the  centralized report, we will have to maintain device database in a CSV  file(format is given in the script), powershell will query each device  for temperature/humidity information and then combine all the  information to an HTML file and mail to the operation/admins provided  the conditions are met.</p>



<p>Steps:</p>



<ol class="wp-block-list"><li>Read the CSV file to get hardware IP, status, location, admin information </li><li>Loop through each device and query for the temperature and humidity data (either HTML or XML) </li><li>Merge all data together as HTML </li><li>Shoot the mail with the HTML as body or as attachment if the  threshold conditions are met (the computer IP should be whitelisted in the SMTP server) </li><li>Schedule the script every 15 minutes to scan all the temperature monitor sensors </li></ol>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p> Note: Below sample code is good if the number of devices are less. When the number of devices are more and scanning frequency is high, then it has to be handled in parallel processing method. Below sample process the sensors one after the other. </p><p><strong> The HTML content varies based on devices</strong></p></blockquote>



<h3 class="wp-block-heading"><strong>PowerShell Script for automating Temperature/Humidity Sensor</strong> <strong>monitoring</strong></h3>



<pre class="wp-block-preformatted">Below Code uses
 · PowerShell Web Methods, XML functions
 · HTML 
 · CSS Styles
 · SMTP mail trigger </pre>



<pre class="wp-block-code"><code lang="powershell" class="language-powershell line-numbers">#######################TEMPERATURE MONITOR REPORTER############################
# CSV STRUCTURE
# IP,Status,Location,AdminInfo
#————————————-
#IP – IP address of the device
#Status – 0 or 1 | flag for whether to scan the device
#Location – Area where device is located
#AdminInfo – Details of Site Administrator
################################

#

##############FUNCTIONS##################

#This method will convert the list to HTML
function frameHTML($__Sensors) {
    $__htmlReport = $__Sensors | ConvertTo-Html
    foreach ($__text in $__htmlReport) {
        if ($__text.substring(1, 2) -eq “tr”) {
            $__temp = ($__htmlReport[$__count].Split(“&gt;”)[6]).split(“&lt;“)[0]
            if ($__heading -eq 0) {
                $__htmlReport[$__count] = $__htmlReport[$__count].Replace(“&lt;tr&gt;”, ‘&lt;tr style=”background-color:lightgreen”&gt;’)
                $__heading = 1
            }

            if ($__temp -eq “DOWN”) {
                $__htmlReport[$__count] = $__htmlReport[$__count].Replace(“&lt;tr&gt;”, ‘&lt;tr style=”background-color:yellow”&gt;’)
            }
            elseif ($__temp -eq “Temperature”) {
                #$__htmlReport[$__count] = $__htmlReport[$__count].Replace(“&lt;tr&gt;”, ‘&lt;tr style=”background-color:blue”&gt;’)
            }

            elseif ([int]$__temp -gt $__MailTriggerTemperatureCritical) {
                $__htmlReport[$__count] = $__htmlReport[$__count].Replace(“&lt;tr&gt;”, ‘&lt;tr style=”background-color:red”&gt;’)
            }

        }
        if ($__text.Contains(“table”)) {
            $__htmlReport[$__count] = $__htmlReport[$__count].Replace(“&lt;table&gt;”, ‘&lt;p style=”text-align: center;”&gt;&lt;span style=”text-decoration: underline;”&gt;&lt;strong&gt;Temperature Monitor&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;table border=”1″&gt;’)
        }
        if ($__text.Contains(“/table”) -and $__Sensors.count -gt 0) {

            $__footerText = @”
&lt;/table&gt;
&lt;p&gt;Note: Rows in &lt;mark class=”red”&gt;RED&lt;/mark&gt;/&lt;mark class=”yellow”&gt;YELLOW&lt;/mark&gt; color, if alerts are fired continuously every 15 minutes, Please inform Admins&lt;/p&gt;
&lt;table style=”width: 298px;” border=”1″&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td style=”width: 30px; background: red”&gt;RED&lt;/td&gt;
&lt;td style=”width: 177px;”&gt;High Temperature Alert&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=”width: 30px; background:yellow”&gt;YELLOW&lt;/td&gt;
&lt;td style=”width: 177px;”&gt;Device Offline&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

“@
            $__htmlReport[$__count] = $__htmlReport[$__count].Replace(“&lt;/table&gt;”, $__footerText)
        }
        if ($__text.Contains(“/head”)) {
            $__CSS = @”
&lt;/head&gt;
&lt;style type=”text/css”&gt;
mark.red {
background: red;
font-weight:bold;
font-style:italic;
}

mark.yellow {
background: yellow;
font-weight:bold;
font-style:italic;
}
&lt;/style&gt;

“@
            $__htmlReport[$__count] = $__htmlReport[$__count].Replace(“&lt;/head&gt;”, $__CSS)
        }
        $__count++
    }
    $__htmlReport | Out-File “C:\Script\TemperatureMonitorDaemon.htm”
}


#This function will trigger the mail
function triggerMail() {
    $fromaddress = “monitor@domain.local”
    $toaddress = “admin1@domain.local”
    $CCaddress = “admin1@domain.local, admin2@domain.local”
    $Bccaddress = “admin1@domain.local, admin2@domain.local”
    $Subject = “Temperature Monitor”
    $body = get-content “C:\Script\TemperatureMonitorDaemon.htm”
    # $attachment = “C:\Script\TemperatureMonitorDaemon.htm”

    $smtpserver = “smptserver.domain.local”

    $message = new-object System.Net.Mail.MailMessage
    $message.From = $fromaddress
    #$message.To.Add($toaddress)
    #$message.CC.Add($CCaddress)
    $message.Bcc.Add($Bccaddress)
    $message.IsBodyHtml = $True
    $message.Subject = $Subject

    $message.body = $body
    $smtp = new-object Net.Mail.SmtpClient($smtpserver)
    $smtp.Send($message)
}

##############END OF FUNCTIONS##################


#This variable will hold the temperature data
$__Sensors = @()

#Threshold for appearing in the mail
$__MailTriggerTemperature = 20

#Threshold for appearing in the mail with red highlight
$__MailTriggerTemperatureCritical = 25

#To keep count of the devices where temperature is above trigger
$__count = 0
$__heading = 0

#importing the hardware ip from CSV file
$__monitors = Import-Csv “C:\Script\DeviceList.txt”
#Filter only devices where flag is 1
$__monitors = $__monitors | where { $_.Status -eq 1 }

#looping through each ip address
$__monitors | % {

    $__DeviceDetails = New-Object PSObject

    #checking for the network connectivity
    if (test-connection $_ -Count 2 -ErrorAction SilentlyContinue) {
        #reading web response data
        $DeviceContent = Invoke-WebRequest -Uri “http://$_/ -TimeoutSec 5 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue

#reading XML data — note that the url is different
$__dev =Invoke-WebRequest -uri “http://$_/data.xml -TimeoutSec 5 -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
        $__xmls = [xml]$__dev.content

        $__TempXML = ($__xmls.server.devices.device.field | where { $_.key -eq “TempC” }).value
        $__HumidityXML = ($__xmls.server.devices.device.field | where { $_.key -eq “Humidity” }).value

        #if the read value is greater than the trigger mentioned in the declared values, then add the details to the list
        if ($__TempXML -gt $__MailTriggerTemperature) {

            $__SensorIP = $_.IP
            #$__SensorData = ($DeviceContent.AllElements | where {$_.class -eq “sensordata_ok”}).innerText
            #$__SensorInfo = ($DeviceContent.AllElements | where {$_.id -eq “footer”} ).outerText

            #if the location is configured in the device and has to be fetched from the same
            #$__Location = ($__SensorInfo.split(“`n”)[0])
            #$__Location = $__Location.Substring(($__Location.IndexOf(“:”) + 2), ($__Location.Length – $__Location.IndexOf(“:”) – 2))

            #info from CSV file
            $__Location = $_.Location
            $__AdminDetails = $_.AdminInfo

            #Adding the sensor information to object
            $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Monitor_IP -Value $__SensorIP
            $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Humidity -Value $__HumidityXML
            $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Temperature -Value $__TempXML
            $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Info -Value $__Location
            $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Admin -Value $__AdminDetails
        }
    }
    else {
        #if the device is not rechable, the same is marked as DOWN
        $__DeviceDetails = New-Object PSObject
        $__SensorIP = $_.IP
        $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Monitor_IP -Value $__SensorIP
        $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Humidity -Value “DOWN”
        $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Temperature -Value “DOWN”
        $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Info -Value “DOWN”
        $__DeviceDetails | Add-Member -MemberType NoteProperty -Name Admin -Value “DOWN”
    }
    #Adding the object to list
    $__Sensors += $__DeviceDetails
}

#convert list to html
frameHTML($__Sensors)


#If the temperature from devices are greater than the trigger temperature, the mails are fired
if ($__Sensors.Count -gt 0) {
    triggerMail
}</code></pre>



<p>Sample Output:</p>



<figure class="wp-block-gallery columns-1 is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex"><ul class="blocks-gallery-grid"><li class="blocks-gallery-item"><figure><img data-recalc-dims="1" decoding="async" src="https://i0.wp.com/tekcookie.com/wp-content/uploads/2019/07/tempmonitor-2.png?w=1080&#038;ssl=1" alt="" data-id="155" data-link="https://adminscripter.wordpress.com/tempmonitor-2/" class="wp-image-155"/></figure></li></ul></figure>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><strong><em>Disclaimer</em></strong>: Please take this only as a reference. I am not responsible for any damage caused to your environment. TEST your code before deploying in production. </p></blockquote>



<div style="height:70px" aria-hidden="true" class="wp-block-spacer"></div>



<p> Hope it will be helpful for you …  </p>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tekcookie.com/monitor-temperature-humidity-sensors-using-powershell/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">149</post-id>	</item>
	</channel>
</rss>
