Visual Studio Community 2013始めました。

Visual Studio Community 2013が出ましたね。Expressでは使えなかった拡張が使えるようになりました。これのためにProの購入を考えては踏み止まって、ということを繰り返していたところなのでやっと来たかという感じ。

真っ先に入れたのがVsVim。お仕事ではPHPStorm + ideavimなので、この環境に慣れ切った指で素のVSでC#を書くのはちょっと腰が重かったのです。

Ctrl+n、Ctrl+pを上下に割り当てて、インテリセンスの選択もカーソルを使わないようにしているのですが、Ctrlを押しているとインテリセンスが半透明になってしまうのが邪魔になっていて回避策を模索中。この機能が付いた当時はとても嬉しかったんですけどね。

あとはPython Tools for Visual Studio。まだしっかりとは試せていないのですが、ClipboardExtenderの拡張スクリプト(IronPython)を書くのが捗りそうです。

MSさん超GJです。

WordPressのxmlrpc.phpへのアクセス数をLogwatchで通知する

WordPressのxmlrpc.phpへの大量アクセスで時々サーバーが息をしなくなってしまうので、IPアドレスでアクセス制限をかけています。ただ、毎回アクセスログを見てアクセス元のIPアドレスを確認するのも面倒です。

日頃からLogwatchでサーバーの状態を確認しているので、ここでxmlrpc.phpへのアクセス数の集計も見られると便利だと思い設定しました。Ubuntu 12.04.4 LTSにtaskselからLAMP環境を構築しWordPressを設置した状態を前提にしています。

まずLogwatchをインストール。

sudo aptitude install logwatch
sudo cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/
sudo cp /usr/share/logwatch/default.conf/services/* /etc/logwatch/conf/services/

/etc/logwatch/conf/logwatch.conf の最低限の設定を変更します。

TmpDir = /tmp
Output = mail

MailTo = myname@example.com

ここで動作確認

sudo /etc/cron.daily/00logwatch

メールが届けばOKです。

/etc/logwatch/conf/services/services に wp-xmlrpc-access-count.conf を作成

Title = "WordPress xmlrpc.php Access Count"
LogFile = http

/etc/logwatch/scripts/services にログの集計を行うスクリプト wp-xmlrpc-access-count を追加

#!/usr/bin/python
import sys;

ipcount = {}
for line in sys.stdin.readlines():
    if "xmlrpc.php" in line:
        ip = line.split(" ", 1)[0]
        if (ip in ipcount):
            ipcount[ip] = ipcount[ip] + 1
        else:
            ipcount[ip] = 1

count_length = 0
for ip, count in sorted(ipcount.items(), key=lambda x:x[1] * -1):
    if count_length == 0:
        count_length = len(str(count))

    print ((ip + ":").ljust(17) + str(count).rjust(count_length) + " Time(s)")

スクリプトの動作確認

logwatch -service wp-xmlrpc-access-count -output stdout

これで集計結果が表示されればOKです。

この設定を行うにあたってLogwatch にサービス・フィルタを追加するを参考にしました。

IronPythonでメソッドをオーバーライドして基底クラスに投げる。

IronPythonに限ったことじゃないけど、IronPythonでWinforms触ってたら避けられない問題なので。組込み関数のsuperを使う。

import clr
clr.AddReference("system.windows.forms")
from System.Windows.Forms import *
class HogeForm(Form):
	WM_LBUTTONDBLCLK = 0x0203
	count = 0

	def WndProc(self, m):
		if m.Msg == self.WM_LBUTTONDBLCLK:
			self.Text = self.count.ToString()
			self.count += 1
		super(HogeForm, self).WndProc(m)

if __name__ == "__main__":
	Application.Run(HogeForm())

 

Form.WndProc(self, m)

でも動くけど推奨されていないみたい。