apache-loggen 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,165 @@
1
+ # はじめに
2
+
3
+ Apacheログに対して何かの処理を行いたい場合に使うためのテストツールです。
4
+ こんな場合に有用なんではないかなと。
5
+
6
+ * productionでもアクセスが足りない
7
+ * 手元でproductionレベルの流速を試したい
8
+ * apacheのログファイルを持ってくるのが面倒
9
+ などなど..
10
+
11
+
12
+ 一応、流速がなだらかになるようになっています。
13
+ 100msよりも割り込み精度が低いOSの場合はその限りではありません。
14
+
15
+ ## 性能
16
+ 私のへっぽこ開発PCで毎秒12,000レコードほど生成します。
17
+
18
+ Fedora16(CPU:2Core, Mem:3GB) on VirtualBox on Windows7(Corei7 M640 2.8GB, Mem:8GB)
19
+ Ruby 1.9.3p194
20
+
21
+ # インストール
22
+
23
+ gem install apache-loggen
24
+
25
+ apache-loggenというコマンドがgems/binに作られます。
26
+
27
+ # 使い方
28
+
29
+ ```
30
+ apache-loggen [options] [file]
31
+
32
+ --limit=COUNT 最大何件出力するか。デフォルトは0で無制限。
33
+ --rate=RATE 毎秒何レコード生成するか。デフォルトは0秒で流量制限無し。
34
+ --rotate=SECOND ファイルローテーションをする間隔。デフォルトは0(行わない)。
35
+ ファイル名を指定した場合は無効。
36
+ --progress STDERRに生成速度の表示をする
37
+ --json Json形式で出力
38
+
39
+ file を指定した場合はそのファイルへ出力する
40
+ file を省略した場合はSTDOUTへ出力する
41
+ ```
42
+
43
+ # 例
44
+
45
+ ## STDOUTにレコード出力
46
+ apache-loggen
47
+
48
+ ## JSONで出力
49
+ apache-loggen --json
50
+
51
+ ## 毎秒100レコードの速度でファイル「abc.log」に出力
52
+ apache-loggen --rate=100 abc.log
53
+
54
+ ## 10秒ごとにファイルのローテーションを行う
55
+ apache-loggen --rotate=10 abc.og
56
+
57
+ ## 生成状況を表示する
58
+ apache-loggen --rotate=10 --progress abc.log
59
+ ----
60
+ file rotate. rename to ./abc.2012-10-17_113723.log
61
+ file rotate. rename to ./abc.2012-10-17_113733.log
62
+ 220[rec] 9.90[rec/s]
63
+
64
+ ## 5000件出力で打ちきる
65
+ apache-loggen --limit=5000
66
+
67
+ # ローテーションルール
68
+ abc.log -> abc.[yyyy-MM-dd_HHmmss].log
69
+
70
+
71
+ # 他のログを生成したい場合
72
+ 再利用できるようにある程度クラス化してあるので、Apache以外のログも対応可能です。
73
+
74
+ 以下のコードでログ生成を開始します。
75
+
76
+ require 'apache-loggen/base'
77
+ LogGenerator.generate(conf=nil, gen_obj=nil, &block)
78
+ ----
79
+ apache-loggen/base をrequireします。
80
+ conf にnilを渡すとARGVをパースします。
81
+ conf にHashを渡すと、デフォルトのオプションから、渡したものだけ上書きします。
82
+
83
+ 実際にログを生成する部分は、LogGenerator::Baseを継承し、generate(context, config)というメソッドを定義する必要があります。
84
+ デフォルトではLogGenerator::Apacheというクラスが存在します。
85
+ Apacheログをベースに何かいじる場合は、これを利用すると良いと思います。
86
+
87
+ もっと気軽に生成したい場合は、以下のようにブロックを渡すことでGeneratorの代わりとなります。
88
+
89
+ LogGenerator.generate() do | context, config, record |
90
+ # ログを1つ分生成する。
91
+ Time.now.to_s + "\n"
92
+ end
93
+
94
+ 上記のrecordは、第2引数のgen_objも指定した場合にその結果を受け取り、さらにblockで加工する場合に使います。
95
+
96
+ ## Apacheのログの出力形式を変更したい
97
+
98
+ ```ruby
99
+ require 'apache-loggen/base'
100
+ class MyGen < LogGenerator::Apache
101
+ def format(record, config)
102
+ # 今回はJSONを無視する
103
+ return %[[#{Time.now.strftime('%d/%b/%Y:%H:%M:%S %z')}] #{record["path"]}\n]
104
+ end
105
+ end
106
+ LogGenerator.generate(nil, MyGen.new)
107
+ ```
108
+
109
+ ## Apacheのログに新しく情報を追加したい
110
+ ```ruby
111
+ require 'apache-loggen/base'
112
+ class MyGen < LogGenerator::Apache
113
+ # オリジナル実装はhashをJSONか1行の文字列にしているが
114
+ # 今回はそれに情報を追加する
115
+ def format(record, config)
116
+ record["process_time"] = grand(1000000) + 1000
117
+ if config[:json] then
118
+ return record.to_json + "\n"
119
+ else
120
+ return %[#{record['host']} - #{record['user']} [#{Time.now.strftime('%d/%b/%Y:%H:%M:%S %z')}] "#{record['method']} #{record['path']} HTTP/1.1" #{record['code
121
+ end
122
+ end
123
+ end
124
+ LogGenerator.generate(nil, MyGen.new)
125
+ ```
126
+
127
+ ## 完全に独自のログ形式を出力したい
128
+
129
+ ```ruby
130
+ require 'apache-loggen/base'
131
+ class MyGen < LogGenerator::Base
132
+ def generate(context, config)
133
+ return "#{Time.now.to_s} #{context.inspect}\n"
134
+ end
135
+ end
136
+ LogGenerator.generate(nil, MyGen.new)
137
+ ```
138
+
139
+ もしくは、
140
+
141
+ ```ruby
142
+ require 'apache-loggen/base'
143
+ LogGenerator.generate do | context, config, record |
144
+ "#{Time.now.to_s} #{context.inspect}\n"
145
+ end
146
+ ```
147
+
148
+
149
+ # 履歴
150
+ - 0.0.2 RubyGemsに登録。コマンドを用意した。クラスの再利用ができるようにした。
151
+ - 0.0.1 はじめてのリリース
152
+
153
+ # ライセンス
154
+ Apache License, Version 2.0
155
+
156
+ # 謝辞
157
+
158
+ TreasureDataのスクリプトをパクりました。
159
+
160
+ オリジナルとの差異は、ログの日付を現在の日付で出力する点です。
161
+
162
+ * https://github.com/treasure-data/td
163
+ * https://github.com/treasure-data/td/blob/master/data/sample_apache_gen.rb
164
+
165
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apache-loggen/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "apache-loggen"
8
+ gem.version = LogGenerator::VERSION
9
+ gem.authors = ["tamtam180"]
10
+ gem.email = ["kirscheless@gmail.com"]
11
+ gem.description = %q{dummy apache-log generator}
12
+ gem.summary = %q{dummy apache-log generator}
13
+ gem.homepage = "https://github.com/tamtam180/apache_log_gen"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'apache-loggen'
@@ -0,0 +1,5 @@
1
+ #coding: utf-8
2
+
3
+ require 'apache-loggen'
4
+ LogGenerator.generate(nil, LogGenerator::Apache.new)
5
+
@@ -0,0 +1,5 @@
1
+ #coding: utf-8
2
+
3
+ require 'apache-loggen/base'
4
+ LogGenerator.generate(nil, LogGenerator::Apache.new)
5
+
@@ -0,0 +1,369 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require 'time'
4
+ require 'fileutils'
5
+ require 'optparse'
6
+ require 'json'
7
+
8
+ module LogGenerator
9
+
10
+ class Base
11
+ def generate(context, config)
12
+ # must override
13
+ exit 1
14
+ end
15
+ end
16
+
17
+ class Apache < Base
18
+
19
+ RECORDS = 5000
20
+ HOSTS = RECORDS/4
21
+ PAGES = RECORDS/4
22
+
23
+ AGENT_LIST_STRING = <<-END
24
+ Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3
25
+ Mozilla/5.0 (iPad; CPU OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3
26
+ Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
27
+ Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
28
+ Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
29
+ Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
30
+ Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
31
+ Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
32
+ Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
33
+ Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
34
+ Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
35
+ Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7
36
+ Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7
37
+ Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
38
+ Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
39
+ Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
40
+ Mozilla/5.0 (Windows NT 6.0; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
41
+ Mozilla/5.0 (Windows NT 6.0; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
42
+ Mozilla/5.0 (Windows NT 6.0; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
43
+ Mozilla/5.0 (Windows NT 6.0; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
44
+ Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
45
+ Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11
46
+ Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11
47
+ Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
48
+ Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
49
+ Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
50
+ Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; YTB730; GTB7.2; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; Media Center PC 6.0)
51
+ Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; YTB730; GTB7.2; EasyBits GO v1.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
52
+ Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB7.2; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
53
+ Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; YTB730; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
54
+ Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; WOW64; Trident/4.0; GTB6; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30618; .NET4.0C)
55
+ Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; YTB720; GTB7.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
56
+ Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS122159; GTB7.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; BRI/2)
57
+ END
58
+ AGENT_LIST = AGENT_LIST_STRING.split("\n").map{|a|a.lstrip}
59
+
60
+ PAGE_CATEGORIES = %w[
61
+ books
62
+ books
63
+ books
64
+ electronics
65
+ electronics
66
+ electronics
67
+ electronics
68
+ electronics
69
+ electronics
70
+ software
71
+ software
72
+ software
73
+ software
74
+ games
75
+ games
76
+ games
77
+ office
78
+ office
79
+ cameras
80
+ computers
81
+ finance
82
+ giftcards
83
+ garden
84
+ health
85
+ music
86
+ sports
87
+ toys
88
+ networking
89
+ jewelry
90
+ ]
91
+
92
+ # インナークラスから使うためにmodule化
93
+ # 使う場合はinclude RANDする
94
+ module RAND
95
+ RANDOM = Random.new
96
+ def grand(n)
97
+ RANDOM.rand(n)
98
+ end
99
+ end
100
+ include RAND
101
+
102
+ def initialize()
103
+
104
+ @pages = []
105
+ PAGES.times do
106
+ @pages << Page.new
107
+ end
108
+
109
+ @hosts = []
110
+ HOSTS.times do
111
+ @hosts << Host.new
112
+ end
113
+
114
+ end
115
+
116
+ def generate(context, config)
117
+
118
+ page = @pages[grand(@pages.size)]
119
+ host = @hosts[grand(@hosts.size)]
120
+ record = {
121
+ 'host' => host.ip,
122
+ 'user' => '-',
123
+ 'method' => page.method,
124
+ 'path' => page.path,
125
+ 'code' => grand(10000) == 0 ? 500 : page.code,
126
+ 'referer' => (grand(2) == 0 ? @pages[grand(@pages.size)].path : page.referer) || '-',
127
+ 'size' => page.size,
128
+ 'agent' => host.agent,
129
+ }
130
+
131
+ return format(record, config)
132
+
133
+ end
134
+
135
+ def format(record, config)
136
+ if config[:json] then
137
+ return record.to_json + "\n"
138
+ else
139
+ return %[#{record['host']} - #{record['user']} [#{Time.now.strftime('%d/%b/%Y:%H:%M:%S %z')}] "#{record['method']} #{record['path']} HTTP/1.1" #{record['code']} #{record['size']} "#{record['referer']}" "#{record['agent']}"\n]
140
+ end
141
+ end
142
+
143
+ class Host
144
+ include RAND
145
+ def initialize
146
+ @ip = "#{(grand(210)+20)/4*4}.#{(grand(210)+20)/3*3}.#{grand(210)+20}.#{grand(210)+20}"
147
+ @agents = []
148
+ end
149
+
150
+ attr_reader :ip
151
+
152
+ def agent
153
+ if @agents.size == 4
154
+ @agents[grand(4)]
155
+ else
156
+ agent = AGENT_LIST[grand(AGENT_LIST.size)]
157
+ @agents << agent
158
+ agent
159
+ end
160
+ end
161
+ end
162
+
163
+ class Page
164
+ include RAND
165
+ def initialize
166
+ cate = PAGE_CATEGORIES[grand(PAGE_CATEGORIES.size)]
167
+ item = grand(RECORDS)
168
+
169
+ if grand(2) == 0
170
+ w = [cate, PAGE_CATEGORIES[grand(PAGE_CATEGORIES.size)]]
171
+ else
172
+ w = [cate]
173
+ end
174
+ q = w.map {|k| k[0].upcase + k[1..-1] }.join('+')
175
+ search_path = "/search/?c=#{q}"
176
+ google_ref = "http://www.google.com/search?ie=UTF-8&q=google&sclient=psy-ab&q=#{q}&oq=#{q}&aq=f&aqi=g-vL1&aql=&pbx=1&bav=on.2,or.r_gc.r_pw.r_qf.,cf.osb&biw=#{grand(5000)}&bih=#{grand(600)}"
177
+
178
+ case grand(12)
179
+ when 0,1,2,3,4,5
180
+ @path = "/category/#{cate}"
181
+ @referers = [nil, nil, nil, nil, nil, nil, nil, google_ref]
182
+ @method = 'GET'
183
+ @code = 200
184
+
185
+ when 6
186
+ @path = "/category/#{cate}?from=#{grand(3)*10}"
187
+ @referers = [search_path, "/category/#{cate}"]
188
+ @method = 'GET'
189
+ @code = 200
190
+
191
+ when 7,8,9,10
192
+ @path = "/item/#{cate}/#{item}"
193
+ @referers = [search_path, search_path, google_ref, "/category/#{cate}"]
194
+ @method = 'GET'
195
+ if grand(100) == 0
196
+ @code = 404
197
+ else
198
+ @code = 200
199
+ end
200
+
201
+ when 11
202
+ @path = search_path
203
+ @referers = [nil]
204
+ @method = 'POST'
205
+ @code = 200
206
+ end
207
+
208
+ @size = grand(100) + 40
209
+ end
210
+
211
+ attr_reader :path, :size, :method, :code
212
+
213
+ def referer
214
+ if grand(2) == 0
215
+ @referers[grand(@referers.size)]
216
+ end
217
+ end
218
+
219
+ end
220
+
221
+ end
222
+
223
+
224
+ # Windowsだと割り込みが55msや10msだったりとするので100msごとに処理するように。
225
+ # 汚いソースになっちゃった・・。
226
+ # MultimediaTimer使えばいいんだけど、めんどくさ。
227
+ class Executors
228
+ FIXED_RATE = 100
229
+ def self.exec(config)
230
+
231
+ rate_per_sec = config[:rate]
232
+ display = config[:progress]
233
+
234
+ limited = rate_per_sec > 0
235
+ if limited then
236
+ mspr = 1000.0 / rate_per_sec # ms per rec.
237
+ rate = rate_per_sec.to_f / (1000 / FIXED_RATE) # rec per 100ms
238
+ end
239
+ start_time = Time.now
240
+
241
+ time = last_display = Time.now
242
+ count = 0
243
+ total_count = 0
244
+ while true do
245
+
246
+ break unless yield({
247
+ :start_time => start_time,
248
+ :total_count => total_count,
249
+ :elapsed_time => (Time.now - start_time).round,
250
+ })
251
+
252
+ total_count += 1
253
+ count += 1
254
+
255
+ if limited && count >= rate then
256
+ spent = ((Time.now - time) * 1000).round
257
+ sleep_ms = mspr - spent
258
+ sleep(sleep_ms / 1000.0) if sleep_ms > 0
259
+ time = Time.now
260
+ count = 0
261
+ end
262
+
263
+ if display then
264
+ if Time.now - last_display >= 1.0 then
265
+ $stderr.printf("\r%d[rec] %.2f[rec/s]", total_count, total_count / (Time.now - start_time + 0.001))
266
+ last_display = Time.now
267
+ end
268
+ end
269
+
270
+ end
271
+ end
272
+ end
273
+
274
+ class MyWriter
275
+ def initialize(filename)
276
+ @filename = filename
277
+ @io = nil
278
+ rotate()
279
+ end
280
+ def rotate()
281
+ if @filename == nil then
282
+ @io = $stdout
283
+ return nil
284
+ else
285
+ dir = File.dirname(@filename)
286
+ name = File.basename(@filename, '.*') + '.' + Time.now.strftime('%Y-%m-%d_%H%M%S') + File.extname(@filename)
287
+ FileUtils.mkdir_p(dir) unless File.exists?(dir)
288
+ if @io != nil then
289
+ File.rename(@filename, name)
290
+ @io.close
291
+ end
292
+ @io = open(@filename, "a")
293
+ return File.join(dir, name)
294
+ end
295
+ end
296
+ def write(str)
297
+ return @io.write(str)
298
+ end
299
+ def close()
300
+ if @filename != nil && @io != nil && !@io.closed? then
301
+ @io.close
302
+ end
303
+ end
304
+ end
305
+
306
+ class Generator
307
+ DEFAULT_CONFIG = {
308
+ :limit => 0,
309
+ :rate => 0,
310
+ :rotate => 0,
311
+ :progress => false,
312
+ :json => false,
313
+ :filename => nil,
314
+ }
315
+ def self.execute(conf={}, gen_obj=nil, &block)
316
+
317
+ config = DEFAULT_CONFIG.merge(conf)
318
+ writer = MyWriter.new(config[:filename])
319
+ gen_kick = gen_obj && gen_obj.is_a?(Base)
320
+
321
+ # 実行
322
+ last_rotate = Time.now.to_i
323
+ Executors.exec(config) do | context |
324
+
325
+ if config[:rotate] > 0 && (last_rotate + config[:rotate]) <= Time.now.to_i then
326
+ rotated_file = writer.rotate()
327
+ if config[:progress] then
328
+ $stderr.write "\rfile rotate. rename to #{rotated_file}\n"
329
+ end
330
+ last_rotate = Time.now.to_i
331
+ end
332
+
333
+ # レコード生成
334
+ record = gen_obj.generate(context, config) if gen_kick
335
+ record = block.call(context, config, record) if block
336
+
337
+ # 出力
338
+ writer.write(record)
339
+
340
+ not (config[:limit] > 0 && config[:limit] <= context[:total_count])
341
+
342
+ end
343
+ writer.close
344
+
345
+ end
346
+ end
347
+
348
+ def parse_config()
349
+ config = {}
350
+ op = OptionParser.new
351
+ op.on('--limit=COUNT', '最大何件出力するか。デフォルトは0で無制限。'){|v| config[:limit] = v.to_i }
352
+ op.on('--rate=RATE', '毎秒何レコード生成するか。デフォルトは0で流量制限無し。'){|v| config[:rate] = v.to_i }
353
+ op.on('--rotate=SECOND', 'ローテーションする間隔。デフォルトは0。'){|v| config[:rotate] = v.to_i }
354
+ op.on('--progress', 'レートの表示をする。'){|v| config[:progress] = true }
355
+ op.on('--json', 'json形式の出力'){|v| config[:json] = true }
356
+ op.parse!(ARGV)
357
+ # ファイルかSTDOUTか
358
+ config[:filename] = ARGV[0] if not ARGV.empty?
359
+ return config
360
+ end
361
+ def generate(conf=nil, gen_obj=nil, &block)
362
+ conf = parse_config() if conf == nil
363
+ Generator.execute(conf, gen_obj, &block)
364
+ end
365
+ module_function :parse_config
366
+ module_function :generate
367
+
368
+ end
369
+
@@ -0,0 +1,3 @@
1
+ module LogGenerator
2
+ VERSION = "0.0.2"
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: apache-loggen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tamtam180
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-15 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: dummy apache-log generator
15
+ email:
16
+ - kirscheless@gmail.com
17
+ executables:
18
+ - apache-loggen
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - apache-loggen.gemspec
28
+ - bin/apache-loggen
29
+ - lib/apache-loggen-boot.rb
30
+ - lib/apache-loggen.rb
31
+ - lib/apache-loggen/base.rb
32
+ - lib/apache-loggen/version.rb
33
+ homepage: https://github.com/tamtam180/apache_log_gen
34
+ licenses: []
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 1.8.24
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: dummy apache-log generator
57
+ test_files: []