LinuxのCPU使用率100%と下げ方

CPU使用率とは、プログラムがどの程度CPUを使っているかとうこと。
CPU使用率が100%でないということは、CPUに遊びがあるという状態。

CPU使用率とは: 一定の時間に対して、どれだけCPUが稼働したか。
1000ミリ秒中550ミリ秒、稼働すれば、CPU使用率は55%になる。当たり前か。

CPU使用率が高いと、アプリケーションのパフォーマンスが落ちるイメージがあるが、つまり、CPU使用率は高い方がいいのか?

CPU周波数とは
1秒間で送信できる0か1のデジタル信号の数
3.2GHzのCPUの場合、Coreで1秒間に32億回0か1の信号を送って処理ができる。つまり、クロック周波数 = 最大通信回数
3.2GHzのCoreに対して、22億4千万回の信号を送って処理すると、Core使用率は70%になる。

で、AWSのCPU周波数は?
M3.Midium Maximum Capacity:3840 MB

DBインスタンスクラスの仕様
https://docs.aws.amazon.com/ja_jp/AmazonRDS/latest/UserGuide/Concepts.DBInstanceClass.html
ECU: Intel XeonまたはAMD Opteronの1.0G~1.2GHz相当(クロック周波数)=最大10~12億通信回数
M3.midiumが3ECU = 3.0G~3.6GHz相当のクロック周波数 = 30~36億通信回数
M3.large(6.5ECU)に上げる?

エンジニアが断捨離するとこうなる(その2)

そろそろ断捨離する物がなくなってきたので、行動面での断捨離を考えたい。
エンジニアは合理的であるべき。とすると、無駄な行動もバッサリ捨てたい。
で、何を捨てたいか?

コンビニ
– コンビニで立ち読みするのが癖で、直したい。 ザファブル、アフロ田中、BLUE GIANT、フルーツ宅急便が気になって仕方がない。
→ 漫画って無意識に思考に影響与えるから、できるだけ読みたくないなー
→ この悩みを解決するソフトウェアを作りたい
案1) コンビニに入ると、GPSで地震警報並みの警報がスマホから流れる
案2) コンビニに入ると、GPSで自動的にクレジットカードで課金されるようにする
案3) rizapのように、毎日コンビニに入った回数を誰かに報告する

あ、GoogleのGoogle Place APIというのがあるらしい。これ、ガチで使ってみたい。
https://developers.google.com/places/

断捨離をプログラミングで解決する、そういう流れにしたいですねー

smtp(postfix)のログの場所

Linuxのsmtpのログの場所は…
/var/log/maillog-yyyymmddにある
apacheのアクセルログ、エラーログと同じ場所にありますね。

では、このmaillog-yyyymmddの中身を見てみましょう。

Dec 17 22:12:09 localhost postfix/postfix-script[4238]: starting the Postfix mail system
Dec 17 22:12:10 localhost postfix/master[4247]: daemon started -- version 2.6.6, configuration /etc/postfix
Dec 18 22:54:04 localhost postfix/postfix-script[15528]: stopping the Postfix mail system
Dec 18 22:54:04 localhost postfix/master[4247]: terminating on signal 15
Dec 20 00:32:55 localhost postfix/postfix-script[1928]: starting the Postfix mail system
Dec 20 00:32:55 localhost postfix/master[1929]: daemon started -- version 2.6.6, configuration /etc/postfix
Dec 22 21:33:09 localhost postfix/postfix-script[1885]: starting the Postfix mail system
Dec 22 21:33:10 localhost postfix/master[1886]: daemon started -- version 2.6.6, configuration /etc/postfix

“starting the Postfix mail system”と、”daemon started — version 2.6.6, configuration /etc/postfix”が多いですね。特に異常なし。

ecmファイルとは‽

.ecmというファイル

ん?なにそれ?

エミュレーター関連でよく使用されるらしい。。。
unecmで解凍できるソフトなどもあるらしい。。。

ecmファイルとは何か?
ECMは、エラーコードモデラー形式で作成したディスクイメージファイル。冗長な誤り訂正符号及びチェックサムを除去し、ディスクイメージを格納する。ゲームコンソールのディスクイメージを圧縮するために使用される。

エラーコードのモデラー形式ってなんだ?
XMAP3/Webライブラリが返すエラーコードが8お場合に、共通インタフェースのリターン値1やリターン2に返すエラーコードの形式を示す。モデラーはモデル。

あんまり情報がないなー

MySQLのログの場所は?

MySQLのログの場所はどこにあるのか??

Mysql:Server version: 5.6.41

MySQLのログファイルの種類は4種類
1. errorログ:サーバから出力されるエラーメッセージを記録
2. SlowQueryログ:処理に時間のかかったクエリを記録
3. 詳細ログ:詳細な情報を記録、全ての操作が記録
4. バイナリログ:更新SQL文のみをバイナリ形式で記録

保存場所は、/etc/my.cnfの中にある。

my.cnfの中身を見てみましょう。
ログファイルの保存場所は、2行目のdatadirで定義されている。
datadir=/var/lib/mysql

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

character_set_server=utf8
default-storage-engine=InnoDB
innodb_file_per_table

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Settings user and group are ignored when systemd is used (fedora >= 15).
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysqld according to the
# instructions in http://fedoraproject.org/wiki/Systemd
user=mysql

# Semisynchronous Replication
# http://dev.mysql.com/doc/refman/5.5/en/replication-semisync.html
# uncomment next line on MASTER
;plugin-load=rpl_semi_sync_master=semisync_master.so
# uncomment next line on SLAVE
;plugin-load=rpl_semi_sync_slave=semisync_slave.so

# Others options for Semisynchronous Replication
;rpl_semi_sync_master_enabled=1
;rpl_semi_sync_master_timeout=10
;rpl_semi_sync_slave_enabled=1

# http://dev.mysql.com/doc/refman/5.5/en/performance-schema.html
;performance_schema

[mysql]
default-character-set=utf8

[mysqldump]
default-character-set=utf8

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid


#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

中を見てみる。あれ? 見れてない??

Laravel セッションドライバ(session driver)

セッションはstorage/framework/sessionsに保存される。

セッションは暗号化され、クッキーに保存される。
セッションはdatabaseに保存される。

開発環境ではfileセッションだが、本番環境ではDBやredisに入れるとあるぞ。

まず、routingから設定します。
routes/web.php

Route::get('/put-data', function(){
	session()->put(['email'=> 'example@gmail.com']);
	return session()->get('email');
});
Route::get('/list-data', function(){
	return session()->all();
});

サーバーを立てます。
[vagrant@localhost zeus]$ php artisan serve –host=192.168.35.10
Laravel development server started:

続いて、/put-dataにアクセスします。

次に、/list-dataにアクセスします。
セッションに保存されているデータがjson形式で表示されます。
{“_token”:”Lc4QKtUxDWzNI2IelKkLcylMfBSB2jgfjbWU8mwD”,”email”:”example@gmail.com”,”_previous”:{“url”:”http:\/\/192.168.35.10:8000\/put-data”},”_flash”:{“old”:[],”new”:[]}}


token, email, _previous, _flashがキーになっていそうですね。

Laravel 5.7のHTTPセッションの設定を見てみる(config.session.php)

セッションとは、ユーザがWebサイトを表示して離脱するまでの一連の流れ
訪問のvisitと同じ

Laravel5.7ではリクエスト間に渡りユーザに関する情報を保存するセッションが提供されている。
Laravel5.7 HTTPセッション

セッションの設定はconfig/session.phpにある。

session.phpの中身を見てみましょう。

return [

    /*
    |--------------------------------------------------------------------------
    | Default Session Driver
    |--------------------------------------------------------------------------
    |
    | This option controls the default session "driver" that will be used on
    | requests. By default, we will use the lightweight native driver but
    | you may specify any of the other wonderful drivers provided here.
    |
    | Supported: "file", "cookie", "database", "apc",
    |            "memcached", "redis", "array"
    |
    */

    'driver' => env('SESSION_DRIVER', 'file'),

    /*
    |--------------------------------------------------------------------------
    | Session Lifetime
    |--------------------------------------------------------------------------
    |
    | Here you may specify the number of minutes that you wish the session
    | to be allowed to remain idle before it expires. If you want them
    | to immediately expire on the browser closing, set that option.
    |
    */

    'lifetime' => env('SESSION_LIFETIME', 120),

    'expire_on_close' => false,

    /*
    |--------------------------------------------------------------------------
    | Session Encryption
    |--------------------------------------------------------------------------
    |
    | This option allows you to easily specify that all of your session data
    | should be encrypted before it is stored. All encryption will be run
    | automatically by Laravel and you can use the Session like normal.
    |
    */

    'encrypt' => false,

    /*
    |--------------------------------------------------------------------------
    | Session File Location
    |--------------------------------------------------------------------------
    |
    | When using the native session driver, we need a location where session
    | files may be stored. A default has been set for you but a different
    | location may be specified. This is only needed for file sessions.
    |
    */

    'files' => storage_path('framework/sessions'),

    /*
    |--------------------------------------------------------------------------
    | Session Database Connection
    |--------------------------------------------------------------------------
    |
    | When using the "database" or "redis" session drivers, you may specify a
    | connection that should be used to manage these sessions. This should
    | correspond to a connection in your database configuration options.
    |
    */

    'connection' => env('SESSION_CONNECTION', null),

    /*
    |--------------------------------------------------------------------------
    | Session Database Table
    |--------------------------------------------------------------------------
    |
    | When using the "database" session driver, you may specify the table we
    | should use to manage the sessions. Of course, a sensible default is
    | provided for you; however, you are free to change this as needed.
    |
    */

    'table' => 'sessions',

    /*
    |--------------------------------------------------------------------------
    | Session Cache Store
    |--------------------------------------------------------------------------
    |
    | When using the "apc" or "memcached" session drivers, you may specify a
    | cache store that should be used for these sessions. This value must
    | correspond with one of the application's configured cache stores.
    |
    */

    'store' => env('SESSION_STORE', null),

    /*
    |--------------------------------------------------------------------------
    | Session Sweeping Lottery
    |--------------------------------------------------------------------------
    |
    | Some session drivers must manually sweep their storage location to get
    | rid of old sessions from storage. Here are the chances that it will
    | happen on a given request. By default, the odds are 2 out of 100.
    |
    */

    'lottery' => [2, 100],

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Name
    |--------------------------------------------------------------------------
    |
    | Here you may change the name of the cookie used to identify a session
    | instance by ID. The name specified here will get used every time a
    | new session cookie is created by the framework for every driver.
    |
    */

    'cookie' => env(
        'SESSION_COOKIE',
        str_slug(env('APP_NAME', 'laravel'), '_').'_session'
    ),

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Path
    |--------------------------------------------------------------------------
    |
    | The session cookie path determines the path for which the cookie will
    | be regarded as available. Typically, this will be the root path of
    | your application but you are free to change this when necessary.
    |
    */

    'path' => '/',

    /*
    |--------------------------------------------------------------------------
    | Session Cookie Domain
    |--------------------------------------------------------------------------
    |
    | Here you may change the domain of the cookie used to identify a session
    | in your application. This will determine which domains the cookie is
    | available to in your application. A sensible default has been set.
    |
    */

    'domain' => env('SESSION_DOMAIN', null),

    /*
    |--------------------------------------------------------------------------
    | HTTPS Only Cookies
    |--------------------------------------------------------------------------
    |
    | By setting this option to true, session cookies will only be sent back
    | to the server if the browser has a HTTPS connection. This will keep
    | the cookie from being sent to you if it can not be done securely.
    |
    */

    'secure' => env('SESSION_SECURE_COOKIE', false),

    /*
    |--------------------------------------------------------------------------
    | HTTP Access Only
    |--------------------------------------------------------------------------
    |
    | Setting this value to true will prevent JavaScript from accessing the
    | value of the cookie and the cookie will only be accessible through
    | the HTTP protocol. You are free to modify this option if needed.
    |
    */

    'http_only' => true,

    /*
    |--------------------------------------------------------------------------
    | Same-Site Cookies
    |--------------------------------------------------------------------------
    |
    | This option determines how your cookies behave when cross-site requests
    | take place, and can be used to mitigate CSRF attacks. By default, we
    | do not enable this as other CSRF protection services are in place.
    |
    | Supported: "lax", "strict"
    |
    */

    'same_site' => null,

];

ドライバー
supported file, cookie, database, apc, memcached, redis, array
‘driver’ => env(‘SESSION_DRIVER’, ‘file’),
セッションライフタイム
‘lifetime’ => env(‘SESSION_LIFETIME’, 120),
‘expire_on_close’ => false,
暗号化
セッションを暗号化できる。
‘encrypt’ => false,
session file location
セッションを保存する場所
‘files’ => storage_path(‘framework/sessions’),
DB connection
‘connection’ => env(‘SESSION_CONNECTION’, null),
sessionを入れるDBテーブル
‘table’ => ‘sessions’,
Session Cache Store
‘store’ => env(‘SESSION_STORE’, null),
‘lottery’ => [2, 100],
session cookie name
‘cookie’ => env(
‘SESSION_COOKIE’,
str_slug(env(‘APP_NAME’, ‘laravel’), ‘_’).’_session’
),
session cookie path
‘path’ => ‘/’,
session cookie domain
‘domain’ => env(‘SESSION_DOMAIN’, null),
HTTPS Only Cookies
‘secure’ => env(‘SESSION_SECURE_COOKIE’, false),
HTTP Access Only
‘http_only’ => true,
Same-Site Cookies
‘same_site’ => null,

apacheのLogLevelディレクティブ

LogLevelディレクティブでは、エラーログに記録するエラーレベルを指定することができる。
以下のように、LogLevel warnでは、”warn”以上のエラーレベルはすべてエラーログに記録するようになり、逆にnotice以下のエラーレベルに関してはログに記載されなくなる。

#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn

ふーん

apacheエラーログの場所とフォーマット

まず、apacheのエラーログの場所は、アクセスログと同様に、
/var/log/httpd/配下にあります

エラーログの中身を見てみましょう。
error_log-yyyymmdd

[Sun Dec 16 16:35:05 2018] [notice] Digest: generating secret for digest authentication ...
[Sun Dec 16 16:35:05 2018] [notice] Digest: done
[Sun Dec 16 16:35:06 2018] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Mon Dec 17 22:12:31 2018] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Mon Dec 17 22:12:32 2018] [notice] Digest: generating secret for digest authentication ...
[Mon Dec 17 22:12:32 2018] [notice] Digest: done
[Mon Dec 17 22:12:42 2018] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Tue Dec 18 22:53:56 2018] [warn] child process 6696 still did not exit, sending a SIGTERM
[Tue Dec 18 22:53:58 2018] [notice] caught SIGTERM, shutting down
[Thu Dec 20 00:32:58 2018] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Thu Dec 20 00:32:58 2018] [notice] Digest: generating secret for digest authentication ...
[Thu Dec 20 00:32:58 2018] [notice] Digest: done
[Thu Dec 20 00:33:03 2018] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Sat Dec 22 21:33:15 2018] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Sat Dec 22 21:33:15 2018] [notice] Digest: generating secret for digest authentication ...
[Sat Dec 22 21:33:15 2018] [notice] Digest: done
[Sat Dec 22 21:33:31 2018] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Sun Dec 23 09:19:04 2018] [notice] SIGHUP received.  Attempting to restart
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName

割と色々出てますが、内容は比較的複雑ではありません。
[Sun Dec 23 09:19:04 2018] :エラー検知日付
[notice] : エラー重要度
SIGHUP received. Attempting to restart
httpd: Could not reliably determine the server’s fully qualified domain name, using localhost.localdomain for ServerName : エラー内容

エラーレベルの種類
emerg: 緊急
alert: ただちに対処が必要
crit: 致命的状態
error: 通常エラー
warn: 警告
notice: 通知
info: サーバーの状態についての情報
debug: デバッグメッセージ

にゃーるほど