Linux 負荷監視コマンド

vstat: システム全体の負荷
top: CPU, メモリを食っているプロセスの特定
sar: 過去の負荷の履歴
mpstat -P ALL 1: CPUコアごとの負荷
dstat -taf 標準でインストールされているコマンド
free -m メモリ
iostat -dmxt 1: IO状況

CPU使用率は $ vmstat で 100 – id の値

$ vmstat
procs ———–memory———- —swap– —–io—- -system– ——cpu—–
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 2193996 89188 931568 0 0 4835 383 460 846 9 10 80 1 0
free, buff, cacheが少ないと問題
us, syがカーネル、カーネル以外のCPU使用率

$ top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
726 root 20 0 874552 38992 20220 S 12.5 1.0 0:01.93 snapd
1 root 20 0 105056 12924 8252 S 0.0 0.3 0:02.41 systemd

$ sudo apt install dstat
$ dstat -taf
Terminal width too small, trimming output.
—-system—- –total-cpu-usage– -net/enp0s3–net/enp0s8 —paging–>
time |usr sys idl wai stl| recv send: recv send| in out >
17-08 06:52:38| 2 1 96 0 0| 0 0 : 0 0 | 0 0 >
17-08 06:52:39| 0 0 100 0 0| 180B 534B: 0 0 | 0 0 >
17-08 06:52:40| 0 1 97 2 0| 120B 220B: 0 0 | 0 0 >
17-08 06:52:41| 0 0 100 0 0| 120B 204B: 0 0 | 0 0 >^

cpu負荷をコアごと、IO負荷をディスクごと、ネットワーク転送量をインターフェイスごとに表示

https://qiita.com/aosho235/items/c4d6995743dd1dac16e1

RDS インスタンスタイプの決め方

db.r5.large などの表記で記載されているが、「r5」の部分がインスタンスタイプ、「large」がインスタンスサイズ

インスタンスタイプは、r系、m系、t系などがある。rdsもt系とm系が汎用で、t系はバースト可能クラス

### r系
・メモリ内の大きいデータセットを処理するワークロードに対して高速なパフォーマンスを実現するように設計されているメモリ最適化インスタンスに属するr系
・r6g.largeのような世代の数字の後ろにgのような文字がついているタイプは追加機能(gならばAWS Gravitonプロセッサ(最良のコストパフォーマンスを提供))
・small < medium < large < xlarge < 2xlarge < 4xlarge < ・・・ なるほど、用途で選ぶのね。

AWS EC2インスタンスタイプの決め方

EC2のインスタンスタイプは用途別に集められた「インスタンスファミリー」とCPUなどのスペックを示す「インスタンスサイズ」から構成される。このインスタンスファミリーの数字は世代を表している。
数字が大きければ大きいほど新しい
t2, t3ではt3の方が新しい

### インスタンスタイプの種類
「汎用」、「コンピューティング最適化」、「メモリ最適化」、「高速コンピューティング」、「ストレージ最適化」の5種類がある
一般的な業務システムに適しているのは「汎用」「コンピューティング最適化」

### 汎用
「T3」「T2」 バーストできる。CPUクレジットを貯めておき、突発的に負荷が高まったときに貯めたCPUクレジットを消費して対応できる。コストが安い

「M5」「M4」 ネットワークをバランスよく提供

### コンピューティング最適化
「C5」「C4」: 性能が高いプロフェッサを積んでいる。高速処理が必須の場合、コストパフォーマンスに優れる。

### 機械学習
GPUインスタンスは、P3

### SAP
X1はSAP から、Business Warehouse on HANA (BW) 、Data Mart Solutions on HANA 、Business Suite on HANA (SoH) 、Business Suite S/4HANA を実行するための認定を受けているインスタンスファミリー

なるほど、勉強になるね。

AWS KMSを使ってみる

AWS KMSコンソールでキーを作成します。

Key policy, Cryptographic configuration, public keyが発行される

public key Publickey-*.pemをダウンロードして使用する。
user nameを設定しているので、ユーザのIDを指定してdecryptするのかな。
概要は何となく理解したが。

AWS KMSとは

データの暗号化/複合化に利用するKeyをセキュアに管理するのがAWS KMSです。
暗号化/複合化のためのキーをAWS KMSで簡単に生成でき、AWS KMS上にキーを保管することができる。

1. KMS上にCMKを作成
暗号化するときに利用するキーを生成するためのCMLを作成

2. CMKのKeyIDからデータキーを作成
CMLを元にしたKeyIDからデータキーを作成する
– 暗号化用のデータキー:データを暗号化するためのキー
– CMKを元に暗号化されたデータキー: データ複合化のために利用するキー

なるほど、使ってみないとわからんな。

FargateのvCPUを理解する

タスクサイズ -> タスクメモリ(MiB)
タスクサイズ -> タスクCPU(単位)
タスクサイズ -> コンテナの定義 -> メモリ制限 -> ハード制限
タスクサイズ -> コンテナの定義 -> メモリ制限 -> ソフト制限
タスクサイズ -> コンテナの定義 -> CPUユニット数

### ハード制限(Memory)
コンテナに適用されるハードウェアのメモリ制限。上限を超えると強制的に終了となる。
ウェブアプリケーションだと300~500MBがおすすめとのこと

### ソフト制限(memoryReservation)
システムメモリ競合時に維持されるメモリ上限 
状況に応じてハード制限(設定されている場合)かインスタンス自体の利用可能上限まで消費
memory (<= memoryReservation) <= instance memory Fargateの動作ベース https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/developerguide/task-cpu-memory-error.html サポートされる CPU 値は、128CPU 単位 (0.125vCPUs) と10240CPU 単位 (10vCPUs)の間 ### CPU value 256(.25vCPU): 512, 1024, 2048 512(.5vCPU): 1024, 2048, 3072, 4096 1024(1vCPU): 2048, 3072, 4096, 5120, 6144, 7168 2048(2vCPU): Between 4096 and 16384 in increments of 1024 4096(4vCPU): Between 8192 and 30720 in increments of 1024 うーむ、何をベースにvCPU, Memoryを決めたら良いかイマイチわからんな。。

[CakePHP3.10] 認証コンポーネント

UsersController.php

use Cake\Auth\DefaultPasswordHasher;
use Cake\Event\Event;

    public function initialize(){
        parent::initialize();
        $this->loadComponent('RequestHandler');
        $this->loadComponent('Flash');
        $this->loadComponent('Auth', [
                'authorize' => ['Controller'],
                'authenticate' => [
                    'Form' => [
                        'fields' => [
                            'username' => 'username',
                            'password' => 'password'
                        ]
                    ]
                ],
                'loginRedirect' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'logoutRedirect' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'logoutRedirect' => [
                    'controller' => 'Users',
                    'action' => 'logout',
                ],
                'authError' => 'ログインしてください。',
            ]);
    }

    function login(){
        if($this->request->isPost()){
            $user = $this->Auth->identify();
            if(!empty($user)){
                $this->Auth->setUser($user);
                return $this->redirect($this->Auth->redirectUrl());
            }
            $this->Flash->error('ユーザ名かパスワードが間違っています。');
        }
    }

    public function logout(){
        $this->request->session()->destroy();
        return $this->redirect($this->Auth->logout());
    }

    public function beforeFilter(Event $event){
        parent::beforeFilter($event);
        $this->Auth->allow(['login', 'index', 'add']);
    }

    public function isAuthorized($user = null){
        if($user['role'] === 'admin'){
            return true;
        }
        if($user['role'] === 'user'){
            return false;
        }
        return false;
    }

authorize, authenticate, loginRedirect, logoutRedirect, authError
$this->request->session()->destroy();

### login.ctpを作成

<div class="users form">
<?= $this->Flash->render('auth') ?>
<?= $this->Form->create() ?>
	<fieldset>
		<legend>アカウント名とパスワードを入力して下さい。</legend>
		<?= $this->Form->input('username') ?>
		<?= $this->Form->input('password') ?>
	<fieldset>
<?= $this->Form->button(__('Login')); ?>
<?= $this->Form->end() ?>
</div>

AuctionBaseController.phpを作成し、継承させる
なるほどー

[CakePHP3.10] auctionサイトを作ろう

$ php composer.phar create-project –prefer-dist cakephp/app:3.* auction

bin/cake bake migration CreateUsers username:string[100] password:string[100] role:string[20]
bin/cake bake migration CreateBiditems user_id:integer name:string[100] finished:boolean endtime:datetime created
bin/cake bake migration CreateBidinfo bititem_id:integer user_id:integer price:integer created
bin/cake bake migration CreateBidrequests biditem_id:integer user_id:integer price:integer created
bin/cake bake migration CreateBidmessages bidinfo_id:integer user_id:integer message:text created
bin/cake migrations migrate
bin/cake bake all users
bin/cake bake all biditems
bin/cake bake all bidinfo
bin/cake bake all bidrequests
bin/cake bake all bidmessages

[CakePHP3.10] ページネーション

– コントローラのページネーション: 一定数ごとにレコードを取り出す
– ビューテンプレートのリンク

controller

$this->loadComponent('Paginator');

PeopleController.php

	public $paginate = [
		'limit' => 5,
		'sort' => 'id',
		'direction' => 'asc',
		'contain' => ['Messages'],
	];

	public function initialize(){
		parent::initialize();
		$this->loadComponent('Paginator');
	}

	public function index(){
		$data = $this->paginate($this->People);
		$this->set('data', $data);
	}

template

<p>This is People table records.</p>
<table>
<thead><tr>
	<th>id</th><th>name</th><th>mail</th><th>age</th><th>message</th>
</tr></thead>
<?php foreach($data->toArray() as $obj): ?>
<tr>
	<td><?=h($obj->id) ?></td>
	<td><a href="<?=$this->Url->build(["controller"=>"People", "action"=>"edit"]); ?>?id=<?=$obj->id ?>"><?=h($obj->name) ?></a></td>
	<td><?=h($obj->mail) ?></td>
	<td><?=h($obj->age) ?></td>
	<td><?php foreach($obj->messages as $item): ?>
	"<?=h($item->message) ?>"<br>
	<?php endforeach; ?></td>
	<td><a href="<?=$this->Url->build(["controller"=>"People", "action"=>"delete"]); ?>?id=<?=$obj->id ?>">delete</a></td>
</tr>
<?php endforeach; ?>
</table>
<div class="paginator">
	<ul class="pagination">
		<?=$this->Paginator->first(' |<< ' . '最初へ') ?>
		<?=$this->Paginator->prev(' << ' . '前へ') ?>
		<?=$this->Paginator->next('次へ ' . ' >> ') ?>
		<?=$this->Paginator->last('最後へ' . ' >>| ') ?>
	</ul>
</div>

custom finderを利用することもできる

なるほど、とりあえずOK 次に行こう