Pippi Life

主に仕事に関連するITのことや、プライベートもちょいちょい書きます。

Windows Server 2012でカスタムメトリクス

気がつけばめっちゃさぼっていました。 Ansible,Terraformも今後載せていこうと思っています。 あとはaws cliとか。

とりあえず、Windows server2012でカスタムメトリクスがはまったので、備忘録的に。

2012の場合、Powershellが無効になっているので、Amazonが提供している方法に追加が必要です・

Get-ExecutionPolicy
Restricted

以下のコマンドを発行し、RremoteSignedにする。 Yを入力しエンターを押す

Set-ExecutionPolicy RemoteSigned

実行ポリシーの変更
実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about_Execution_Policies
のヘルプ トピック (http://go.microsoft.com/fwlink/?LinkID=135170)
で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか?
[Y] はい(Y)  [N] いいえ(N)  [S] 中断(S)  [?] ヘルプ (既定値は "Y"): Y

確認

Get-ExecutionPolicy
RemoteSigned

あとはサイトの提供する通りに。 →https://docs.aws.amazon.com/ja_jp/AmazonCloudWatch/latest/DeveloperGuide/mon-scripts-powershell.html

S3に圧縮ファイルを格納する

S3は堅牢性が高いので、 ログの保管などに便利です。

例えばlogrotateを設定して 1日ごとにファイルを切り替えて圧縮したりとか。 で、圧縮ファイルをS3に格納する。

#!/bin/sh
#s3へファイルを移動する。
#引数:第1引数 ファイルのフィルタリング名フルパス
#   第2引数 バケット名

echo "start...";

#引数を変数に格納する。
S3_from=$1
S3_to=$2

#対象ファイルの抽出
targetfiles=`ls ${S3_from}`

#それぞれの対象について処理をする。
for i in `echo ${targetfiles}`
do
  #s3へ移動させる。
  aws s3 mv ${i} ${S3_to}
done

echo "end...";

なんかシンプルすぎるなwwww あ、エラー処理は省きました。

CentOS 7にMySQL5.6.22をインストールする。

仕事でCMSを使う機会があって、MySQLを導入する必要がありました。

ということで、MySQL導入します。

MariaDBの何かしらがインストールされていないか確認

yum list installed | grep maria

入っていました。

mariadb-libs.x86_64              1:5.5.35-3.el7                        @anaconda

MySQLを入れるのでアンインストール

yum -y remove mariadb-libs

MySQLのインストール

#コミュニティのリポジトリを追加
yum -y install http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

#それぞれ追加していきましょう。
yum -y install mysql
yum -y install mysql-devel
yum -y install mysql-server
yum -y install mysql-utilities

mysqlデーモンの起動コマンド

service mysqld start

DB初期設定はこのコマンド

/usr/bin/mysql_secure_installation

そうするとこんなん出てきます。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):    ←確か空エンター
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y    ←rootのパスワードを設定するか?yes!
New password:
Re-enter new password:      ←パスワードを入力
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y       ←アノニマスユーザー削除する?yes!
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y   ←rootのリモート接続はできないようにする?yes!
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y  ←テストDB削除する?yes!
 - Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
 ... Failed!  Not critical, keep moving...
 - Removing privileges on test database...
 ... Success!
#なんかエラー出てるけど、まあいいか!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y     ←権限設定をリロードしとく?yes!
 ... Success!




All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!


Cleaning up...

接続してみましょう。

# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.1.69 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql
mysql show databases;

無事に接続されました。

梅雨ですが、ベランダは元気です。

私の家はマンションの3階にあるのですが、日当たりはあまりよくないです。 しかしなにか作りたいという気持ちが募り募った結果、今ベランダに4つのプランターが並んでいますw

バジル、ゴーヤ、トマト、モロヘイヤ。

初夏になって、バジルが元気な季節になりました。 バジルは生命力が強いのか、水をあげる以外放置してもどんどん葉っぱを増やしてくれます。 葉っぱは摘み取って、市販のトマトソースと一緒にとか、絶品ですよ。

100均の安いソースとパスタでも、バジルをたっぷりいれるだけでさわやかな美味しさが!! さらにチーズを入れたらもっと幸せな味に!

秋冬になったら残念ながら枯れ落ちてしまうらしいですが、 夏のあいだにバジルは使い切れないほど生い茂る「らしい」ので、乾燥させれば冬も使えそう!!ww

LINUXでCLOUDWATCHのカスタムメトリクス導入

AWSのCloudwatchは標準で以下のものが見れます。 あ、CloudwatchってAWSモニタリングサービスです。

CPU使用率 ・ディスクI/O量・回数 ・ネットワーク情報量 ・状態 ってことで、デフォルトにはディスク使用率やメモリがないんですね。 なので手動で設定する必要があります。

以下が、設定方法です。

 

Amazon Linux にログインして、以下のコマンドを発行。

sudo yum install perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https

だだだっと流れた後に以下が出ればOKです!

Complete!

次に以下のコマンドでAmazon CloudWatch モニタリングスクリプトをダウンロードします。

wget http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip

 

これを解凍すると、ディレクトリが作成されるので、配置する場所で実施すれば良いです。

unzip CloudWatchMonitoringScripts-1.2.1.zip
rm CloudWatchMonitoringScripts-1.2.1.zip
cd aws-scripts-mon

以下のコマンドを発行してみましょう。--verifyついていると、お試し実行です。

./mon-put-instance-data.pl --disk-space-util --disk-path=/ <span style="color: #ff0000;">--verify</span> --verbose

これとか

Successfully reported metrics to CloudWatch. Reference Id: ~~~

とかでたならOK!

Verification completed successfully. No actual metrics sent to CloudWatch.

こうなるのはエラーです。

ちなみに、CentOSとかだと出たように記憶しています。

Digest/SHAがないよと怒られています。

 

Can't locate Digest/SHA.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 . .) at AwsSignatureV4.pm line 23.
BEGIN failed--compilation aborted at AwsSignatureV4.pm line 23.
Compilation failed in require at CloudWatchClient.pm line 21.
BEGIN failed--compilation aborted at CloudWatchClient.pm line 21.
Compilation failed in require at ./mon-put-instance-data.pl line 86.
BEGIN failed--compilation aborted at ./mon-put-instance-data.pl line 86.

その時はこれ

sudo yum install -y perl-Digest-SHA

カスタムメトリクスは定期的にコマンド発行して、データをAWS上に格納していく必要があります。

クーロンを設定してみます。

crontab -e

以下を追加

*/5 * * * * /home/ec2-user/aws-scripts-mon/mon-put-instance-data.pl --disk-space-util --disk-path=/ --verbose
*/5 * * * * /home/centos/aws-scripts-mon/mon-put-instance-data.pl --mem-util --disk-space-util --disk-path=/

うん、いい感じ!

アフェリエイトもちょっと始めてみました(笑) オフィシャルでちょくちょくお世話になっていますが、ここのクーポンはお得(^_^)

クラウドソーシング「ランサーズ」

AWS Cloudwatchのキャッシュ

AWSのCludwatchのカスタムメトリクスを使うためのmon-get-instance-stats.pl はキャッシュを作成して、それをインスタンスIDとしてデータをAWS上にputします。なので

インスタンス作成

→Cludwatch使う

インスタンスのAMI取得

インスタンス削除

→取得したAMIからインスタンスローンチ

すると、古いインスタンスIDでCludwatchにデータ収集されてしまいます。まぁ、それでドハマりしたんですけども。。。

 

/var/tmp/aws-mon配下にある、

instance-id

placement/availability-zone

aws configureに設定する内容が入っています。

こいつを削除すればOK。

cd /var/tmp
rm -rf aws-mon

AMIからAutoScalingするときとか、Cloudinitなんかで削除するとかしないとダメですね。