2014/05/18

Amazon LinuxのデフォルトのEPELを利用しようとしてエラーが出るときは…

Amazon のよくある質問にも書いてあるのですが、

Q: Extra Packages for Enterprise Linux(EPEL)リポジトリを有効にするにはどうすればよいですか?

これに従って yum --enablerepo=epel install hogehoge とかやると、

One of the configured repositories failed (不明),and yum doesn't have enough cached data to continue. At this point the onlysafe thing yum can do is fail. There are a few ways to work "fix" this:

Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again

なんて言われてEPELが利用出来ないときは、/etc/yum.repos.d/epel.repo を
修正してあげるとすんなり直ります。

# vi /etc/yum.repos.d/epel.repo

[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

↑ もともとコメントアウトされている baseurl のコメントを外して、
 代わりに mirrorlist をコメントアウトする。

2014/05/12

PowerShellを使って、WMI経由でPingをたたく

WMIにはいくつか面白いオブジェクトがあるので使ってみる。

Win32_PingStatus : 指定されたアドレスにPingを飛ばして詳細な結果オブジェクトを返す


試しに www.google.com にPingを投げてみるには、

PS C:\Users\imksoo> $ping_status = Get-WmiObject -Namespace "root\cimv2" -Query "SELECT * FROM Win32_PingStatus WHERE Address='www.google.com'"

PS C:\Users\imksoo> $ping_status | Get-Member


   TypeName: System.Management.ManagementObject#root\cimv2\Win32_PingStatus

Name                           MemberType     Definition
----                           ----------     ----------
PSComputerName                 AliasProperty  PSComputerName = __SERVER
Address                        Property       string Address {get;set;}
BufferSize                     Property       uint32 BufferSize {get;set;}
NoFragmentation                Property       bool NoFragmentation {get;set;}
PrimaryAddressResolutionStatus Property       uint32 PrimaryAddressResolutionStatus {get;set;}
ProtocolAddress                Property       string ProtocolAddress {get;set;}
ProtocolAddressResolved        Property       string ProtocolAddressResolved {get;set;}
RecordRoute                    Property       uint32 RecordRoute {get;set;}
ReplyInconsistency             Property       bool ReplyInconsistency {get;set;}
ReplySize                      Property       uint32 ReplySize {get;set;}
ResolveAddressNames            Property       bool ResolveAddressNames {get;set;}
ResponseTime                   Property       uint32 ResponseTime {get;set;}
ResponseTimeToLive             Property       uint32 ResponseTimeToLive {get;set;}
RouteRecord                    Property       string[] RouteRecord {get;set;}
RouteRecordResolved            Property       string[] RouteRecordResolved {get;set;}
SourceRoute                    Property       string SourceRoute {get;set;}
SourceRouteType                Property       uint32 SourceRouteType {get;set;}
StatusCode                     Property       uint32 StatusCode {get;set;}
Timeout                        Property       uint32 Timeout {get;set;}
TimeStampRecord                Property       uint32[] TimeStampRecord {get;set;}
TimeStampRecordAddress         Property       string[] TimeStampRecordAddress {get;set;}
TimeStampRecordAddressResolved Property       string[] TimeStampRecordAddressResolved {get;set;}
TimestampRoute                 Property       uint32 TimestampRoute {get;set;}
TimeToLive                     Property       uint32 TimeToLive {get;set;}
TypeofService                  Property       uint32 TypeofService {get;set;}
__CLASS                        Property       string __CLASS {get;set;}
__DERIVATION                   Property       string[] __DERIVATION {get;set;}
__DYNASTY                      Property       string __DYNASTY {get;set;}
__GENUS                        Property       int __GENUS {get;set;}
__NAMESPACE                    Property       string __NAMESPACE {get;set;}
__PATH                         Property       string __PATH {get;set;}
__PROPERTY_COUNT               Property       int __PROPERTY_COUNT {get;set;}
__RELPATH                      Property       string __RELPATH {get;set;}
__SERVER                       Property       string __SERVER {get;set;}
__SUPERCLASS                   Property       string __SUPERCLASS {get;set;}
ConvertFromDateTime            ScriptMethod   System.Object ConvertFromDateTime();
ConvertToDateTime              ScriptMethod   System.Object ConvertToDateTime();
IPV4Address                    ScriptProperty System.Object IPV4Address {get=$iphost = [System.Net.Dns]::GetHostEntr...
IPV6Address                    ScriptProperty System.Object IPV6Address {get=$iphost = [System.Net.Dns]::GetHostEntr...

PS C:\Users\imksoo> $status | Format-Table -AutoSize @{Label="Date";Expression={Get-Date -Format "G"}},Address,ResponseTime,ResponseTimeToLive,IPV4Address,IPV6Address

Date                Address        ResponseTime ResponseTimeToLive IPV4Address     IPV6Address
----                -------        ------------ ------------------ -----------     -----------
2014/05/12 11:17:13 www.google.com           81                 52 173.194.117.177

1秒おきにPingを打ち続けるとかであれば、以下の様にFormat-Tableで現在時刻を付与しながら表示してあげると、後々整形しやすいかと。
(ExPingもどきを目指すのであれば、タイムアウト値とかをGet-WmiObjectコマンドレットでPingStatusオブジェクトを作るときに渡しておくとかでしょうか)

PS C:\Users\imksoo> while ( -not (Start-Sleep 1) ) {
>>  $ping_status = Get-WmiObject -Namespace "root\cimv2" -Query "SELECT * FROM Win32_PingStatus WHERE Address='www.googl
e.com'"
>>  $ping_status | Format-Table -AutoSize @{Label="Date";Expression={Get-Date -Format "G"}},Address,ResponseTime,Respons
eTimeToLive,IPV4Address,IPV6Address
>> }
>>

Date                Address        ResponseTime ResponseTimeToLive IPV4Address     IPV6Address
----                -------        ------------ ------------------ -----------     -----------
2014/05/12 11:19:40 www.google.com          112                 52 173.194.117.177

Date                Address        ResponseTime ResponseTimeToLive IPV4Address     IPV6Address
----                -------        ------------ ------------------ -----------     -----------
2014/05/12 11:19:41 www.google.com           94                 52 173.194.117.177

Date                Address        ResponseTime ResponseTimeToLive IPV4Address     IPV6Address
----                -------        ------------ ------------------ -----------     -----------
2014/05/12 11:19:42 www.google.com          179                 52 173.194.117.177

Date                Address        ResponseTime ResponseTimeToLive IPV4Address     IPV6Address
----                -------        ------------ ------------------ -----------     -----------
2014/05/12 11:19:43 www.google.com          203                 52 173.194.117.177

2014/05/07

Windows Server 2008上のPowerShellでHyper-Vの仮想マシンの状態一覧を出すコマンド

WMI+VBScript経由で取得するサンプルスクリプトはいくつか見つけたんですが、PowerShell版はうまく動かなかったりするので書き直したものを残しておきます。

VMStateは仮想マシンの状態を文字列に戻すためのハッシュ変数。
Msvm_ComputerSystemが戻すオブジェクトは仮想マシン+ホストマシンの一覧になっていて、EnabledStateが実際のマシン状態を表す整数値(と見せかけて文字列)を返してくる。
※ EnabledStateをそのままVMState変数に突っ込むと"2"みたいな文字列でハッシュを当たりに行くので、[int]$_.EnabledStateでint型に変換してからVMStateに当たりに行くのが書き換えたポイント。

$VMState=@{
  0="Unknown" ;
  2="Running" ;
  3="Stopped" ;
  32768="Paused" ;
  32769="Suspended";
  32270="Starting" ;
  32771="Snapshotting" ;
  32773="Saving" ;
  32774="Stopping" ;
  32776="Pausing" ;
  32777="Resuming"

Get-WmiObject -Namespace "root\virtualization" -ComputerName "." -Class Msvm_ComputerSystem | Format-Table -autosize @{Label="CurrentDate"; expression={Get-Date -Format "G"}}, @{Label="ElementName"; expression={$_.ElementName}}, @{Label ="EnabledState"; expression={$VMState[[int]$_.EnabledState]}}, Caption, Description

PS C:\Windows\system32> $VMState=@{
  0="Unknown" ;
  2="Running" ;
  3="Stopped" ;
  32768="Paused" ;
  32769="Suspended";
  32270="Starting" ;
  32771="Snapshotting" ;
  32773="Saving" ;
  32774="Stopping" ;
  32776="Pausing" ;
  32777="Resuming"


PS C:\Windows\system32> Get-WmiObject -Namespace "root\virtualization" -ComputerName "." -Class Msvm_ComputerSystem | Format-Table -autosize @{Label="CurrentDate"; expression={Get-Date -Format "G"}}, @{Label="ElementName"; expression={$_.ElementName}}, @{Label ="EnabledState"; expression={$VMState[[int]$_.EnabledState]}}, Caption, Description

CurrentDate         ElementName EnabledState Caption                        Description
-----------         ----------- ------------ -------                        -----------
2014/05/07 11:53:11 TESTHV01    Running      ホスト コンピューター システム Microsoft ホスト コンピューター システム
2014/05/07 11:53:11 HOGEHOGE    Stopped      仮想マシン                     Microsoft Virtual Machine
2014/05/07 11:53:11 TESTPC1     Stopped      仮想マシン                     Microsoft Virtual Machine

2014/05/07 11:53:11 TESTPC2     Running      仮想マシン                     Microsoft Virtual Machine


Windows Server 2012 以降であれば、標準搭載のHyper-VコマンドレットでGet-VM一発で終わるんですけどねぇ…。

PS C:\Users\imksoo> Get-VM

Name               State CPUUsage(%) MemoryAssigned(M) Uptime   Status
----               ----- ----------- ----------------- ------   ------
CentOS6.2-minimal  Off   0           0                 00:00:00 正常稼働中
CentOS6.2-minimal2 Off   0           0                 00:00:00 正常稼働中
CentOS6.3          Off   0           0                 00:00:00 正常稼働中
Chef-Server        Off   0           0                 00:00:00 正常稼働中