Powershell 更新 Nagios Windows客户端

简介:

豆子今天在一个新配置的Nagios服务器上发现对Windows服务器的内存检查仍然使用的是check_nt, check_nt检查的是物理内存和虚拟内存之和;而我需要查看的只是物理内存,因此我需要改成check_nrpe。 Nagios服务器上的配置不多说了,下载安装相关插件,配置command.cfg和windows.cfg, 然后客户端修改对应的NSClient的配置文件,测试通过。


然后问题来了,我有80多台Windows服务器需要修改对应的配置文件,还有10多台Windows服务器根本就没有安装NSClient,我可不想手动去一一安装配置,写了个简单的脚本统一修改。


基本思路,从AD里面获取Windows服务器名字,判断是否在线。对于在线的服务器进行扫描,判断是否安装了NSClient;如果已经安装过的,判断是否已经备份,否则备份之后更新配置文件;如果没有安装的,拷贝安装文件到本地的temp文件夹,进行安装然后更新配置文件。


值得一提的有几点:

1.使用invoke-command远程操作的时候,默认的安全机制会禁止访问网络资源!因此我需要把MSI文件拷贝到本地才能安装。

2.Powershell里面使用msiexec安装msi文件的时候,需要使用start-process 的格式,列如

1
start-Process  -FilePath msiexec.exe -ArgumentList  "/i c:\temp\NSCP.msi /q"  -Wait -PassThru

如果是远程使用,需要注意第一不能访问网络资源,第二不能使用交互式安装,第三这个MSI文件本身不能创建任何和用户档案相关的文件,比如快捷方式等等;任何一条违反了都会导致失败。

3. 拷贝文件的时候我使用了 copy ... | out-null 的格式,这个作用和 start-process copy.exe -wait 是一样的,都是为了保证当前命令成功结束之后才会进行下一条命令。

4. 这个脚本写的并不完善,比如我没有对有对操作系统是64位或者32位进行判断,也没有异常报错处理等等,我稍后有空进行完善,不过已经可以处理我需要的内容了。


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
Write-Host  "Scanning Online Servers ..."
$a = get-adcomputer  - filter  {operatingsystem  -like  "*20*" }
$computers =@()
foreach  ( $b  in  $a  ){
if ( Test-Connection  -computername  $b .name -Count 1 -Quiet){
$temp = [psobject] @{ 'name' = $b .name}
$computers += $temp
}
}
Write-Host  "Scanning Nagios Clients ..."
$c2 =@()
$computers  ForEach-Object  {
$path = "\\" + $_ .name+ "\c$\Program Files\NSClient++\nsclient.ini"
$bakpath = "\\" + $_ .name+ "\c$\Program Files\NSClient++\nsclient.ini.bak"
 
if  (( Test-Path  -Path  $path -and  !( Test-Path  -Path  $bakpath ))
{
 
  copy  $path  $bakpath
  copy  "\\sydav01\c`$\program files\NSClient++\nsclient.ini"  $path 
  #"Restart nscp service on "+$_.name
  Invoke-Command  -ComputerName  $_ .name { restart-service  nscp}
} else
{
$path  " Folder doesn't Esixt"
$temp = [psobject] @{'name '=$_.name}
$c2+=$temp
}
}
$end=$false
while ( $end -eq $false){
Write-Host "Following servers don' t have Nagios Client Installed. "
$c2 .name
$option read-host  "Do you want to Install ? ( Y/N ) "
switch ( $option )
{
"Y"
     $c2 foreach-object  {
     $path2 = "\\" + $_ .name+ "\c$\temp\NSCP.msi"
     if Test-Path  $path2 ){}
     else  {
     New-Item  $path2  -Force
     }
     Write-host  "Copying NSCP.msi files to " $path2
     copy  '\\sydit01\c$\Temp\NSCP-0.4.4.15-x64.msi'  $path2  Out-Null
     Write-host  "Copying is completed and start to install"
     Invoke-Command  -ComputerName  $_ .name -ScriptBlock {
     Start-Process  -FilePath msiexec.exe -ArgumentList  "/i c:\temp\NSCP.msi /q"  -Wait -PassThru
     }
     $path3 = "\\" + $_ .name+ "\c$\Program Files\NSClient++\nsclient.ini"
     Write-host  "Installation is completed and now is updting config file"
     copy  "\\sydav01\c$\program files\NSClient++\nsclient.ini"  $path3
     Invoke-Command  -ComputerName  $_ .name { restart-service  nscp}
}
$end = $true ;
}
"N" {
     $end = $true
     }
default {
     "Please answer Y or N"
}
}
}


运行片段,拷贝文件到某台服务器,安装,更新文件,重启服务

wKiom1aysEuTBlnyAACitSwNka4317.png


从Nagios服务器上测试成功获取内存信息


wKioL1aysKCxbLuVAAC774ualUE960.png










本文转自 beanxyz 51CTO博客,原文链接:http://blog.51cto.com/beanxyz/1741051,如需转载请自行联系原作者

目录
相关文章
|
4月前
|
Kubernetes Linux Windows
第二章 Linux和windows部署helm 客户端
第二章 Linux和windows部署helm 客户端
132 0
|
4月前
|
Windows
Coze开源软件Windows客户端-coze_desk
Coze开源软件Windows客户端-coze_desk
779 0
|
4天前
|
Windows
Windows操作系统部署安装Kerberos客户端
详细介绍了在Windows操作系统上部署安装Kerberos客户端的完整过程,包括下载安装包、安装步骤、自定义安装路径、修改环境变量、配置hosts文件和Kerberos配置文件,以及安装后的验证步骤。
16 3
Windows操作系统部署安装Kerberos客户端
|
3月前
|
网络架构 Windows
windows电脑开机自动启动FRP客户端CMD代码,增加延时15秒功能
经常使用FRP客户端的都知道,FRP客户端启动时需要电脑有网络,否则会自动退出。我们的电脑在启动时,受路由器网络的影响,很多时间并不能第一时间建立起网络来,所以很容易受到网络影响,让FRP客户端自动启动发生故障。而这里的解决办法就是增加一段代码,可以延时15秒再执行。
|
3月前
|
C# Windows
一个免费、时尚、强大的 Windows GitHub 客户端
一个免费、时尚、强大的 Windows GitHub 客户端
|
4月前
|
Apache 项目管理 数据安全/隐私保护
Windows安装TortoiseSVN客户端结合Cpolar实现公网提交文件到本地服务器
Windows安装TortoiseSVN客户端结合Cpolar实现公网提交文件到本地服务器
|
4月前
|
Apache 项目管理 数据安全/隐私保护
Windows如何安装使用TortoiseSVN客户端并实现公网访问本地SVN Server
Windows如何安装使用TortoiseSVN客户端并实现公网访问本地SVN Server
66 0
|
4月前
|
Apache 数据安全/隐私保护 Windows
如何在Windows部署TortoiseSVN客户端并实现公网连接内网VisualSVN服务端
如何在Windows部署TortoiseSVN客户端并实现公网连接内网VisualSVN服务端
|
4月前
|
Windows
EVE-NG的Windows客户端安装
EVE-NG提供Windows的客户端,集成了Wireshark、VNC、putty等软件,主要为完成配套EVE-NG的WEB浏览中的数据抓包等功能。 我的EVE-NG的Windows客户端安装包为EVE-NG-Win-Client-Pack.exe。
|
4月前
|
安全 物联网 Linux
Windows客户端操作系统的历史版本简介
从Windows 1.0到最新的Windows 11,微软操作系统经历了从简单的GUI层迭于DOS之上,到全面独立且功能丰富的现代操作系统的发展历程,不断引入创新的用户界面、提升性能、强化安全性,并随着技术进步与市场需求变化,逐步优化了对多任务处理、网络连接、移动和触控设备的支持。
313 0
下一篇
DDNS