2016/11/20

CentOS 7のインストールメモ

CentOS 7をMinimal Installした後に、Ruby 2.3.2を入れるところまで。

# Install Packages.
sudo yum update -y
sudo yum groups install -y 'Development Tools'
sudo yum install -y epel-release
sudo yum install -y ansible git vim tmux zsh

# Change login shell.
chsh -s /bin/zsh

# Install rbenv.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zshrc
~/.rbenv/bin/rbenv init
vim ~/.zshrc
 eval "$(rbenv init -)"
type rbenv

# Install rbenv-build.
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

# Install Ruby 2.3.2
sudo yum install -y openssl-devel readline-devel zlib-devel
rbenv install -l
rbenv install 2.3.2

rbenv version
rbenv versions
rbenv local 2.3.2

ruby -v

2015/10/29

RDS MySQLのログファイルをまとめてきれいにダウンロードする

RDS MySQLのインスタンスがクラッシュしたりしたときに mysql-error.log や mysql-general.log を一括してダウンロードしたいときに使うコマンドのメモ。

■mysql-errorログのダウンロード

## インスタンス名を指定
$ DB_INSTANCE_NAME=rds-test-instance

## RDSログ一覧を取得→ログの更新日時で並び替え→ログファイルのサイズが0以上→errorを含むログを抽出→一括ダウンロード
$ aws rds describe-db-log-files --db-instance-identifier ${DB_INSTANCE_NAME} \
   | jq -r '.DescribeDBLogFiles | sort_by(.LastWritten)[] | select(.Size>0) | .LogFileName' \
   | grep 'error' \
   | xargs -I% \
     aws rds download-db-log-file-portion --db-instance-identifier ${DB_INSTANCE_NAME} --log-file-name % --output text \
     > mysql-error.log

■mysql-generalログのダウンロード

## インスタンス名を指定
$ DB_INSTANCE_NAME=rds-test-instance

## RDSログ一覧を取得→ログの更新日時で並び替え→ログファイルのサイズが0以上→generalを含むログを抽出→一括ダウンロード
$ aws rds describe-db-log-files --db-instance-identifier ${DB_INSTANCE_NAME} \
   | jq -r '.DescribeDBLogFiles | sort_by(.LastWritten)[] | select(.Size>0) | .LogFileName' \
   | grep 'general' \
   | xargs -I% \
     aws rds download-db-log-file-portion --db-instance-identifier ${DB_INSTANCE_NAME} --log-file-name % --output text \
     > mysql-general.log

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 正常稼働中

2014/04/07

Pacemaker + cmanでクラスタを作る手順メモ

日曜日の夜が更けてきたのでコマンドだけメモして寝るw

# vi /etc/hosts
10.3.12.121     LAINCL01.local LAINCL01
10.3.12.122     LAINCL02.local LAINCL02


# yum install pacemaker corosync pcs cman ccs
# corosync-keygen
# cd /etc/corosync/
# scp authkey root@10.3.12.122:/etc/corosync
# cp corosync.conf.example.udpu corosync.conf
# vi corosync.conf
以下の設定を記載
・interface memberにノードIPを並べる
・bindnetaddrはネットワークアドレス
・transportはudpuにする
・logging to_syslogはnoにする


# iptables -I INPUT 1 --protocol udp --dport 5405 -j ACCEPT
# iptables -I INPUT 1 --protocol udp --sport 5404 -j ACCEPT
# iptables -I OUTPUT 1 --protocol udp --dport 5405 -j ACCEPT
# iptables -I OUTPUT 1 --protocol udp --sport 5404 -j ACCEPT


# crm_mon

# service cman start
# service pacemaker start
# chkconfig cman on
# chkconfig pacemaker on

# ccs -f /etc/cluster/cluster.conf --createcluster LAINCL00
# ccs -f /etc/cluster/cluster.conf --addnode LAINCL01.local
# ccs -f /etc/cluster/cluster.conf --addnode LAINCL02.local
# ccs -f /etc/cluster/cluster.conf --addfencedev pcmk agent=fence_pcmk
# ccs -f /etc/cluster/cluster.conf --addmethod pcmk-redirect LAINCL01.local
# ccs -f /etc/cluster/cluster.conf --addmethod pcmk-redirect LAINCL02.local
# ccs -f /etc/cluster/cluster.conf --addfenceinst pcmk LAINCL01.local pcmk-redirect port=LAINCL01.local
# ccs -f /etc/cluster/cluster.conf --addfenceinst pcmk LAINCL02.local pcmk-redirect port=LAINCL02.local

# echo "CMAN_QUORUM_TIMEOUT=0" >> /etc/sysconfig/cman

# pcs property set stonith-enabled=false
# pcs property set no-quorum-policy=ignore

参考:
http://clusterlabs.org/quickstart-redhat.html
http://blog.suz-lab.com/2012/12/corosync-pacemaker.html
http://linux-ha.sourceforge.jp/wp/dl/pminstall_cent5
http://sios-oss.blogspot.jp/2013/05/pacemaker-corosync-ha.html

2014/02/07

Windows + PowerShellで特定の引数を持ったプロセスを見つけて、メモリ使用量を取るコマンド

書くほどではないがメモとして。

PowerShellではGet-Processコマンドレットでプロセス一覧が取得出来る。
ただ、それだとコマンドライン引数が分からないため、「hoge.jarを動かしているjava.exe」みたいなプロセスを特定することが出来ない。

そのためGet-WmiObject経由で Win32_Process オブジェクトを使ってコマンドライン引数を含めた情報を取得してみた。

Get-WmiObjectを利用した場合

PS C:\> Get-WmiObject Win32_Process | ? { $_.Name -eq 'notepad.exe' -and $_.CommandLine -ilike '*test.txt*' }


__GENUS                    : 2
__CLASS                    : Win32_Process
__SUPERCLASS               : CIM_Process
__DYNASTY                  : CIM_ManagedSystemElement
__RELPATH                  : Win32_Process.Handle="5124"
__PROPERTY_COUNT           : 45
__DERIVATION               : {CIM_Process, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER                   : SL20807-W8
__NAMESPACE                : root\cimv2
__PATH                     : \\SL20807-W8\root\cimv2:Win32_Process.Handle="5124"
Caption                    : notepad.exe
CommandLine                : "C:\WINDOWS\system32\notepad.exe" c:\test.txt
CreationClassName          : Win32_Process
CreationDate               : 20140207164215.299105+540
CSCreationClassName        : Win32_ComputerSystem
CSName                     : SL20807-W8
Description                : notepad.exe
ExecutablePath             : C:\WINDOWS\system32\notepad.exe
ExecutionState             :
Handle                     : 5124
HandleCount                : 122
InstallDate                :
KernelModeTime             : 1093750
MaximumWorkingSetSize      : 1380
MinimumWorkingSetSize      : 200
Name                       : notepad.exe
OSCreationClassName        : Win32_OperatingSystem
OSName                     : Microsoft Windows 8.1 Pro|C:\WINDOWS|\Device\Harddisk0\Partition2
OtherOperationCount        : 343
OtherTransferCount         : 866
PageFaults                 : 8697
PageFileUsage              : 4368
ParentProcessId            : 7260
PeakPageFileUsage          : 4572
PeakVirtualSize            : 173985792
PeakWorkingSetSize         : 13676
Priority                   : 8
PrivatePageCount           : 4472832
ProcessId                  : 5124
QuotaNonPagedPoolUsage     : 12
QuotaPagedPoolUsage        : 311
QuotaPeakNonPagedPoolUsage : 14
QuotaPeakPagedPoolUsage    : 330
ReadOperationCount         : 31
ReadTransferCount          : 23364
SessionId                  : 6
Status                     :
TerminationDate            :
ThreadCount                : 1
UserModeTime               : 312500
VirtualSize                : 167088128
WindowsVersion             : 6.3.9600
WorkingSetSize             : 13975552
WriteOperationCount        : 0
WriteTransferCount         : 0
PSComputerName             : SL20807-W8
ProcessName                : notepad.exe
Handles                    : 122
VM                         : 167088128
WS                         : 13975552
Path                       : C:\WINDOWS\system32\notepad.exe

別解:WMICコマンドを使った場合

C:\> wmic.exe process where "(Name='notepad.exe') and (CommandLine like '%test.txt%') list /format:list

CommandLine="C:\WINDOWS\system32\notepad.exe" c:\test.txt
CSName=SL20807-W8
Description=notepad.exe
ExecutablePath=C:\WINDOWS\system32\notepad.exe
ExecutionState=
Handle=5124
HandleCount=122
InstallDate=
KernelModeTime=1093750
MaximumWorkingSetSize=1380
MinimumWorkingSetSize=200
Name=notepad.exe
OSName=Microsoft Windows 8.1 Pro|C:\WINDOWS|\Device\Harddisk0\Partition2
OtherOperationCount=343
OtherTransferCount=866
PageFaults=8697
PageFileUsage=4368
ParentProcessId=7260
PeakPageFileUsage=4572
PeakVirtualSize=173985792
PeakWorkingSetSize=13676
Priority=8
PrivatePageCount=4472832
ProcessId=5124
QuotaNonPagedPoolUsage=12
QuotaPagedPoolUsage=311
QuotaPeakNonPagedPoolUsage=14
QuotaPeakPagedPoolUsage=330
ReadOperationCount=31
ReadTransferCount=23364
SessionId=6
Status=
TerminationDate=
ThreadCount=1
UserModeTime=312500
VirtualSize=167088128
WindowsVersion=6.3.9600
WorkingSetSize=13975552
WriteOperationCount=0
WriteTransferCount=0

遠回りだけど何とかパフォーマンスカウンタでやりたい場合

ちなみに、typeperfコマンドやパフォーマンスカウンタを使いたい場合、対象となるインスタンスを特定するために一工夫が必要になる。

エレガントにやる手順は Find a Performance Counter Instance by a Process ID に詳しいのでそちらを参照してみてください。
キモはPowerShellだと ImportCSV コマンドレットでCSVデータを名前付きオブジェクトで扱えるから、それでプロセスIDを元にインスタンス名を特定できるところ。