PowerShell 发送美观的Vsphere DataStore警报
豆子今天登陆Vsphere VCenter的时候,无意中发现有DataStore的警报信息,个别DataStore的使用空间超过90%了,需要清空一下SAN Volume的Snapshot。这个是运维常见的问题,那么顺便就用PowerShell写个脚本,定期检查发送邮件好了。
脚本本身很容易,但是我想让他尽量的美观一些。
之前我写过一个博文可以自定义sytle的样式 http://beanxyz.blog.51cto.com/5570417/1786712, 不过现在看起来有些麻烦,还是觉得找找如果有比较好看的现成的css文件可以直接调用就好了。
上网搜了搜 table有哪些现成的css模板,随便找了一个https://codepen.io/anon/pen/vJmLWL,看着还成
下载他的css下来
下载的css文件,保存在C:\tmp 目录
@import url(https://fonts.googleapis.com/css?family=Roboto:400,500,700,300,100); body { background-color: #3e94ec; font-family: "Roboto", helvetica, arial, sans-serif; font-size: 16px; font-weight: 400; text-rendering: optimizeLegibility; } div.table-title { display: block; margin: auto; max-width: 600px; padding:5px; width: 100%; } .table-title h3 { color: #fafafa; font-size: 30px; font-weight: 400; font-style:normal; font-family: "Roboto", helvetica, arial, sans-serif; text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1); text-transform:uppercase; } /*** Table Styles **/ .table-fill { background: white; border-radius:3px; border-collapse: collapse; height: 200px; margin: auto; max-width: 600px; padding:5px; width: 100%; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1); animation: float 5s infinite; } th { color:#D5DDE5;; background:#1b1e24; border-bottom:4px solid #9ea7af; border-right: 1px solid #343a45; font-size:23px; font-weight: 100; padding:24px; text-align:left; text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1); vertical-align:middle; } th:first-child { border-top-left-radius:3px; } th:last-child { border-top-right-radius:3px; border-right:none; } tr { border-top: 1px solid #C1C3D1; border-bottom-: 1px solid #C1C3D1; color:#666B85; font-size:16px; font-weight:normal; text-shadow: 0 1px 1px rgba(256, 256, 256, 0.1); } tr:hover td { background:#4E5066; color:#FFFFFF; border-top: 1px solid #22262e; border-bottom: 1px solid #22262e; } tr:first-child { border-top:none; } tr:last-child { border-bottom:none; } tr:nth-child(odd) td { background:#EBEBEB; } tr:nth-child(odd):hover td { background:#4E5066; } tr:last-child td:first-child { border-bottom-left-radius:3px; } tr:last-child td:last-child { border-bottom-right-radius:3px; } td { background:#FFFFFF; padding:20px; text-align:left; vertical-align:middle; font-weight:300; font-size:18px; text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1); border-right: 1px solid #C1C3D1; } td:last-child { border-right: 0px; } th.text-left { text-align: left; } th.text-center { text-align: center; } th.text-right { text-align: right; } td.text-left { text-align: left; } td.text-center { text-align: center; } td.text-right { text-align: right; }
下面是正式的脚本,Set-CellColor也是别人写好的现成的,我直接拿来用了,主要功能是根据条件来更改html文件table的格子的颜色,比如某个值大于警报线就标记为红色等等。脚本中间加载Vsphere SnapIn,查询datastore的状态,最后把结果转换为html,通过Office365发送邮件出去
#修改颜色块的设定 Function Set-CellColor { [CmdletBinding()] Param ( [Parameter(Mandatory,Position=0)] [string]$Property, [Parameter(Mandatory,Position=1)] [string]$Color, [Parameter(Mandatory,ValueFromPipeline)] [Object[]]$InputObject, [Parameter(Mandatory)] [string]$Filter, [switch]$Row ) Begin { Write-Verbose "$(Get-Date): Function Set-CellColor begins" If ($Filter) { If ($Filter.ToUpper().IndexOf($Property.ToUpper()) -ge 0) { $Filter = $Filter.ToUpper().Replace($Property.ToUpper(),"`$Value") Try { [scriptblock]$Filter = [scriptblock]::Create($Filter) } Catch { Write-Warning "$(Get-Date): ""$Filter"" caused an error, stopping script!" Write-Warning $Error[0] Exit } } Else { Write-Warning "Could not locate $Property in the Filter, which is required. Filter: $Filter" Exit } } } Process { ForEach ($Line in $InputObject) { If ($Line.IndexOf("<tr><th") -ge 0) { Write-Verbose "$(Get-Date): Processing headers..." $Search = $Line | Select-String -Pattern '<th ?[a-z\-:;"=]*>(.*?)<\/th>' -AllMatches $Index = 0 ForEach ($Match in $Search.Matches) { If ($Match.Groups[1].Value -eq $Property) { Break } $Index ++ } If ($Index -eq $Search.Matches.Count) { Write-Warning "$(Get-Date): Unable to locate property: $Property in table header" Exit } Write-Verbose "$(Get-Date): $Property column found at index: $Index" } If ($Line -match "<tr( style=""background-color:.+?"")?><td") { $Search = $Line | Select-String -Pattern '<td ?[a-z\-:;"=]*>(.*?)<\/td>' -AllMatches $Value = $Search.Matches[$Index].Groups[1].Value -as [double] If (-not $Value) { $Value = $Search.Matches[$Index].Groups[1].Value } If (Invoke-Command $Filter) { If ($Row) { Write-Verbose "$(Get-Date): Criteria met! Changing row to $Color..." If ($Line -match "<tr style=""background-color:(.+?)"">") { $Line = $Line -replace "<tr style=""background-color:$($Matches[1])","<tr style=""background-color:$Color" } Else { $Line = $Line.Replace("<tr>","<tr style=""background-color:$Color"">") } } Else { Write-Verbose "$(Get-Date): Criteria met! Changing cell to $Color..." $Line = $Line.Replace($Search.Matches[$Index].Value,"<td style=""background-color:$Color"">$Value</td>") } } } Write-Output $Line } } End { Write-Verbose "$(Get-Date): Function Set-CellColor completed" } } #加载Vsphere function Load-PowerCLI { #pls download and install module first Add-PSSnapin VMware.VimAutomation.Core # Add-PSSnapin VMware.VimAutomation.Vds # Add-PSSnapin VMware.VumAutomation . "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1" } #判断是否已经加载SnapIn $snapins=Get-PSSnapin if($snapins.name -eq "VMware.VimAutomation.Core") { Write-Host "Vsphere SnapIn is loaded" -ForegroundColor Cyan } else{ Load-PowerCLI } #绑定VCenter Connect-VIServer sydvcs2012 #获取DataStore的数据 $report = @() foreach($cluster in Get-Cluster){ Get-VMHost -Location $cluster | Get-Datastore | %{ $info = "" | select DataCenter, Cluster, Name, Capacity, Free, 'UsagePercentage(%)' $info.Datacenter = $_.Datacenter $info.Cluster = $cluster.Name $info.Name = $_.Name $info.Capacity = [math]::Round($_.capacityMB/1024,2) $info.Free="{0:N1}" -f $_.FreeSpaceGB $info.'UsagePercentage(%)'=[math]::round(100*($_.CapacityGB-$_.FreeSpaceGB)/$_.CapacityGB,2) $report += $info } } #通过Office365发送邮件 $from = "aaa@abc.com" $to = "bean@abc.com" $smtp = "smtp.office365.com" $sub = "DataStore list" $secpasswd = ConvertTo-SecureString "Password" -AsPlainText -Force $mycreds = New-Object System.Management.Automation.PSCredential ($from, $secpasswd) #指定css模板转换数据为html格式,根据条件指定cell的颜色 $htmlbody=$report| ConvertTo-Html -Body "<H1> DataStore </H1>" -CssUri C:\tmp\table.css | Set-CellColor -Property "UsagePercentage(%)" -Color red -Filter "UsagePercentage(%) -gt 80" #发送邮件 Send-MailMessage -To $to -From $from -Subject $sub -Body ($htmlbody|Out-String) -Credential $mycreds -SmtpServer $smtp -DeliveryNotificationOption Never -BodyAsHtml -UseSsl -port 587
我收到的邮件效果
最后添加脚本到task scheduler里面让他每日自动运行就行了

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
RabbitMQ入门与使用篇
介绍 RabbitMQ是一个由erlang开发的基于AMQP(Advanced Message Queue)协议的开源实现。用于在分布式系统中存储转发消息,在易用性、扩展性、高可用性等方面都非常的优秀。是当前最主流的消息中间件之一。 RabbitMQ的官方 概念: Brocker:消息队列服务器实体。 Exchange:消息交换机,指定消息按什么规则,路由到哪个队列。 Queue:消息队列,每个消息都会被投入到一个或者多个队列里。 Binding:绑定,它的作用是把exchange和queue按照路由规则binding起来。 Routing Key:路由关键字,exchange根据这个关键字进行消息投递。 Vhost:虚拟主机,一个broker里可以开设多个vhost,用作不用用户的权限分离。 Producer:消息生产者,就是投递消息的程序。 Consumer:消息消费者,就是接受消息的程序。 Channel:消息通道,在客户端的每个连接里,可建立多个channel,每个channel代表一个会话任务。 消息队列的使用过程大概如下: 消息接收 客户端连接到消息队列服务器,打开一个ch...
- 下一篇
MySQL DBA必备工具使用的6大锦囊妙计
老张我呢不仅是个金庸迷,还是个三国迷。就是喜欢看后期蜀国诸葛亮与魏国司马懿之间的斗智斗勇。各种锦囊妙计的使用,堪称经典。针对管理MySQL数据库这块,张老师也有很多妙计,今后一一给大家介绍。说回三国,我个人更倾向于蜀国可以统一,但事与愿违,很可惜,最终还是魏国司马炎统一了天下。有人把蜀国失败的原因归结于一个扶不起的刘婵,也有人把原因归结于天命,更有甚者说是"卧龙凤雏得其一"才可得天下,而刘备两人兼得了。现在听听很可笑,其实任何人的命运还都是掌握在自己手中的。 我们要学会尽人事知天命,努力去做好每一件事儿,不放过一个小小的细节。尤其是从事数据库这个领域,更要细致细心。曾经我的一位老师跟我说过,你要学会把你从事的工作,融入到自己的血液当中去。只有真正地爱上它,才能去用心去研究它! 每次老张写博之前,都喜欢说一些心灵鸡汤,不爱听的老铁们,也希望你们见谅!其实就是希望大家能够用心去做每一件事儿,不管在哪个行业,你早晚会成功。 老张的 MySQL 网络课程部分也在51CTO学院正式上线了,想学习的同学们可以去访问 有任何问题都可以及时跟老师沟通。 今儿给大家分享一篇,关于MySQL DBA必备工...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2全家桶,快速入门学习开发网站教程
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- 2048小游戏-低调大师作品
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS6,CentOS7官方镜像安装Oracle11G
- CentOS关闭SELinux安全模块