webネタ

Webエンジニアが業務に関係することをメモしていく

OAuthの覚え書きメモ

OAuth1.0覚えたのでメモ。

1、RequestTokenを取得するリクエスト。
2、redirectしてそのサービスでログインしてリダイレクトして戻ってくる(コールバック)。
3、RequestTokenを認証(Authorized)するリクエスト。
4、AccessTokenを取得するリクエスト。
5、4で取得したtokenを使って、色々とapi叩く。

Zend_Oauth_Consumerがとても便利。OAuth1.0のみ対応。
これを使うと4のAccessTokenを取得するリクエストを送る前に、RequestTokenの認証を内部的に行ってくれる。

てかfacebookはOAuth2.0だし・・・

apacheのMaxClientを算出するスクリプトを作った

概要

apacheの設定にあるMaxClientsは、サーバースペック等から算出できるため、毎回手動で計算するのは面倒なのでスクリプトを作った。

計算方法

(サーバーメモリ量) / (httpd使用メモリ量 - httpd使用共有メモリ量) = MaxClients

psコマンドではなく、/proc/PID/smapsから取得しているためより正確。コマンドのみで算出しようと思ったが、平均値とか出すのが面倒だったためスクリプトにしてみた。でも、あくまで目安ということで。

参考

基本的にここを参考にさせていただきました。

使い方

/usr/bin/php get_max_client.php

ソースコード (get_max_client.php)

<?php

//--------------------------------------------------------------------------------------
// Usage: php get_max_client.php
// root only.
// 
// This program is used to get MaxClient of apache.
// Following step.
// 1、get all pid of httpd.
// 2、get average Rss of "/proc/$pid/smaps".
// 3、get average Shared_Clean of "/proc/$pid/smaps".
// 4、get average Shared_Dirty of "/proc/$pid/smaps".
// 5、get memory size by free command.
// 6、math max client by "memorySize / (rssAverage - (Shared_Clean + Shared_Dirty))".
// 7、print max client.
//--------------------------------------------------------------------------------------


$GLOBALS['debug'] = false;


/**
 * Dump data.
 * If $GLOBALS['debug'] is true, dump.
 *
 * @param mixd $value
 * @param string $label
 */
function debugDump ($value, $label = null) {
    if ($GLOBALS['debug']) {
        if ($label) {
            $value = $label . ' : ' . $value;
        }
        var_dump($value);
    }
}

/**
 * command execute. error handling and logging.
 * if command is error, it will end. (exit(1))
 *
 * @param string $arg
 * @return string
 */
function executeCommand ($arg)
{
    $result = `$arg 2>&1`;

    if (substr(`echo $?`, 0, 1) === '0') {
        // success
        return rtrim($result);

    } else {
        // failure
        error_log(sprintf('command failure [%s][%s]', $arg, $result));
        echo 0;
        exit(1);
    }
}


/**
 * This method is run.
 * If you want to see debug data, see this page top.
 *
 */
function run ()
{
    $httpdPidArr = getHttpdPidArr();
    $rss = getRssAverage($httpdPidArr);
    $shr = getShrAverage($httpdPidArr);
    $memorySize = getMemorySize();
    $maxClient = mathMaxClient($shr, $rss, $memorySize);

    debugDump($rss, '$rss');
    debugDump($shr, '$shr');
    debugDump($memorySize, '$memorySize');
    debugDump($maxClient, '$maxClient');
    
    echo sprintf('--------------------------------') . PHP_EOL;
    echo sprintf('memorySize / (rssAverage - shrAverage) = %dKB / (%dKB - %dKB) = %d', $memorySize, $rss, $shr, $maxClient) . PHP_EOL;
    echo sprintf('MaxClient maximum value is %d.', $maxClient) . PHP_EOL;
    echo sprintf('--------------------------------') . PHP_EOL;
}

/**
 * Get httpd pid.
 *
 * @return array
 */
function getHttpdPidArr ()
{
    $httpdPids = executeCommand('pgrep httpd');
    if (empty($httpdPids)) {
        throw new Exception("Not found httpdPids. failure pgrep httpd.");
    }
    return  explode(PHP_EOL, $httpdPids);
}

/**
 * Get rss.
 * This method is browse /proc/$pid/smaps.
 *
 * @return int
 */
function getRss ($pid)
{
    $rss = executeCommand("cat /proc/$pid/smaps | grep Rss | awk '{rss += $2} END {print rss;};'");
    if (empty($rss)) {
        throw new Exception("Rss not found. /proc/$pid/smaps not found?.");
    }
    return (int) $rss;
}

/**
 * Get rss average. 
 *
 * @return int
 */
function getRssAverage ($httpdPidArr)
{
    foreach ($httpdPidArr as $httpdPid) {
        $rssArr[] = $debug = getRss($httpdPid);
    }

    return (int) round(array_sum($rssArr) / count($rssArr));
}

/**
 * Get rss.
 * This method is browse /proc/$pid/smaps.
 *
 * @return int
 */
function getShrClean ($pid)
{
    $shrClean = executeCommand("cat /proc/$pid/smaps | grep Shared_Clean | awk '{shrc += $2} END {print shrc;};'");
    if (empty($shrClean)) {
        throw new Exception("Shared_Clean not found. /proc/$pid/smaps not found?.");
    }
    return (int) $shrClean;
}

/**
 * Get rss.
 * This method is browse /proc/$pid/smaps.
 *
 * @return int
 */
function getShrDirty ($pid)
{
    $shrDirty = executeCommand("cat /proc/$pid/smaps | grep Shared_Dirty | awk '{shrd += $2} END {print shrd;};'");
    if (empty($shrDirty)) {
        throw new Exception("Shared_Dirty not found. /proc/$pid/smaps not found?.");
    }
    return (int) $shrDirty;
}

/**
 * Get shr average.
 * (Shared_Clean + Shared_Dirty) / count
 *
 * @return int
 */
function getShrAverage ($httpdPidArr)
{
    foreach ($httpdPidArr as $httpdPid) {
        $shrClean = getShrClean($httpdPid);
        $shrDirty = getShrDirty($httpdPid);

        debugDump("pid=$httpdPid : shrClean $shrClean");
        debugDump($shrDirty, '$shrDirty');

        $shrArr[] = $shrClean + $shrDirty;
    }

    return (int) round(array_sum($shrArr) / count($shrArr));
}

/**
 * Get memory size.
 * This method is execute free command.
 *
 * @return int
 */
function getMemorySize ()
{
    $result = executeCommand("free | grep Mem | awk '{print $2;};'");
    if (empty($result)) {
        throw new Exception('memory size not found. failure free command...');
    }
    return $result;
}

/**
 * Math MaxClient.
 *
 * @param int $shr
 * @param int $rss
 * @param int $memorySize
 * @return int MaxClient
 */
function mathMaxClient ($shr, $rss, $memorySize)
{
    return (int) round($memorySize / ($rss - $shr));
}


run();

linuxコマンドの履歴についてのまとめ

historyコマンド

history

いっぱいコマンドの履歴が出てきます。

historyコマンドの履歴に時間を記録

export HISTTIMEFORMAT='%y/%m/%d %H:%M:%S '
export HISTSIZE=10000
実行例
id
ls
cd /var/www/vhosts/
less /etc/php.ini
結果例
1010  2011-02-07 14:42:33 id
1011  2011-02-07 14:42:33 ls
1012  2011-02-07 14:42:33 cd /var/www/vhosts/
1013  2011-02-07 14:42:35 less /etc/php.ini

historyコマンドで参照してるファイル

~/.bash_history

psacctを使って全ユーザーのコマンドを記録

yum install psacct
service psacct start
lastcomm
lastcomm --user ryo
実行例
id
ls
cd /var/www/vhosts/
less /etc/php.ini
結果例
[root@localhost ~]# lastcomm --user ryo | head
less                    ryo      pts/2      0.01 secs Mon Feb  7 14:42
lesspipe.sh             ryo      pts/2      0.01 secs Mon Feb  7 14:42
ls                      ryo      pts/2      0.00 secs Mon Feb  7 14:42
id                      ryo      pts/2      0.00 secs Mon Feb  7 14:42

んー微妙に使えない・・・


おまけ

mysqlのコマンド履歴

~/.mysql_history

grant all on *.* to sample@localhost identified by 'password';
mysqlユーザー作成のクエリ発行した後等は、履歴をクリアしないと・・・・


トラバ(・ω・)
http://blog.justoneplanet.info/2011/02/07/mac%e3%81%aexampp%e3%81%abxdebug%e3%82%92%e5%85%a5%e3%82%8c%e3%82%8b/trackback/

シェルでechoの文字に色をつける方法

echo -e "\033[0;31mテキスト\033[0;39m"

とするとテキストという文字が赤で表示される。

以下の太字になっている部分を変えることで、様々な色に変えられる。

echo -e "\033[0;31mテキスト\033[0;39m"
文字色のカラーバリエーション
  • 30 => 黒 : Black
  • 31 => 赤 : Red
  • 32 => 緑 : Green
  • 33 => 黄色 : Yellow
  • 34 => 青 : Blue
  • 35 => マゼンダ : Magenta
  • 36 => シアン : Cyan
  • 37 => 白 : White
文字背景のカラーバリエーション
  • 40 => 黒 : Black
  • 41 => 赤 : Red
  • 42 => 緑 : Green
  • 43 => 黄色 : Yellow
  • 44 => 青 : Blue
  • 45 => マゼンダ : Magenta
  • 46 => シアン : Cyan
  • 47 => 白 : White
以下の太字部分を変えれば、太字や下線も適用可能。
echo -e "\033[0;31mテキスト\033[0;39m"
  • 0 => ノーマル : All attributs off
  • 1 => 太字 : Bold on
  • 4 => 下線 : Underscore (on monochrome display adapter only)
  • 5 => 点滅 : Blink on
  • 7 => 色反転 Reverse video on
  • 8 => Concealed on


参考
http://ascii-table.com/ansi-escape-sequences.php

FlashLite1.1 メモリ使用量の調査したよ

テスト内容

  • 1、画像の種類でメモリ使用量がどう変わるか。
  • 2、変数の定義数でメモリ使用量がどう変わるか。

対象環境

  • FlashLite1.1
  • ドキュメントサイズ w240 x h320
  • SH823 (softbank)

メモリ使用量の計測方法

_root.mem_use = fscommand2("GetTotalPlayerMemory") - fscommand2("GetFreePlayerMemory");

テスト実施

1、jpg, 透過png, 透過gifでどの程度メモリ消費に違いがあるか調査

クリックして1枚ずつ5枚の画像を表示し、そのつどメモリ消費量を取得した。

jpg
  • 1枚目 = 144KBメモリ消費した
  • 2枚目 = 116KB
  • 3枚目 = 116KB
  • 4枚目 = 116KB
  • 5枚目 = 116KB

swfファイルサイズ = 85.4KB

透過png
  • 1枚目 = 116KB
  • 2枚目 = 104KB
  • 3枚目 = 68KB
  • 4枚目 = 68KB
  • 5枚目 = 68KB

swfファイルサイズ = 87.4KB

透過gif
  • 1枚目 = 68KB
  • 2枚目 = 36KB
  • 3枚目 = 20KB
  • 4枚目 = 20KB
  • 5枚目 = 20KB

swfファイルサイズ = 23.6KB

まとめ

gifが一番ファイル容量もメモリ使用量も軽い。
gifパネー


2、変数10個定義, 11個, 12個, 100個, 200個, 300個, 400個, 500個した場合のメモリ消費量の違い
変数10個定義した場合

結果 : 141KB
swfファイルサイズ : 431B

変数11個定義した場合

結果 : 141KB
swfファイルサイズ : 451B

変数12個定義した場合

結果 : 141KB
swfファイルサイズ : 471B

変数100個定義した場合

結果 : 157KB
swfファイルサイズ : 2,233B (2KB)

変数200個定義した場合

結果 : 209KB
swfファイルサイズ : 4,433B (4KB)

変数300個定義した場合

結果 : 221KB
swfファイルサイズ : 6,633B (6KB)

変数400個定義した場合

結果 : 249KB
swfファイルサイズ : 8,833B (8KB)

変数500個定義した場合

結果 : 289KB
swfファイルサイズ : 11,033B (11KB)

まとめ

変数一つ追加するごとに
メモリ : +300B (100個追加したとき平均34KB増えるので、1/100として)
swfファイルサイズ : +20B


結果のまとめ

写真ならjpeg、それ以外はgifを使用し、できるだけpngは使用しない方向で開発しないと画像を多用する場合はメモリ消費が激しいので厳しい。変数は、フラグや1-9の数値などであれば工夫次第で一つの変数に入れれる。たとえば、val=(変数1の値)(変数2の値)(変数3の値)と考えれば、val=111と固定の数値ならsubstring()などで指定の位置から値を持ってくるだけで変数を定義しなくてよくなる。さらに変数の文字数も短くするとさらにメモリも軽量化できる。flashlite1.1でそれなりのものを作るならばかなりシビアに節約したコードを書かなければならなくなりそうだ。

P.S.
使用したflaやswfは言ってもらえれば差し上げます。UPするのがめんどうですた。。

サーバー監視ツール比較のまとめ

Linuxサーバーの監視ツールどれかいいか調べたのでまとめる。

有名どこ

  • Nagios
  • Hobbit
  • ZABBIX
  • Hinemos

ふむ

zabbixが一番よさそう(´艸` )