azure-loganalytics-datacollector-api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: daf49dbf92029301348d64dc66385db82a7c130d
4
+ data.tar.gz: 6fd642535b7b1a12d4053f211e9308a144bf8022
5
+ SHA512:
6
+ metadata.gz: ab57e162e60914424738f67094b812b1c87f6669bf70a08b67c7dbdaa057f747cb2f4f321a1126a755e25368776a3750137db394e1efb830de14ce64caafe015
7
+ data.tar.gz: 3c98b7dba31bb69b6e3fa3c562a807f28d1b23017ca521b19f9c94652a943973eacfa7a10b7009c719d9f3a930fb743f13adba84c91f09d55341ae5310d8ada0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in azure-loganalytics-datacollector-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yoichi Kawasaki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Azure::Loganalytics::Datacollectorapi
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/azure/loganalytics/datacollectorapi/client`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'azure-loganalytics-datacollector-api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install azure-loganalytics-datacollector-api
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/azure-loganalytics-datacollectorapi-client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default do
7
+ system("rake -T")
8
+ end
9
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'azure/loganalytics/datacollectorapi/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "azure-loganalytics-datacollector-api"
8
+ spec.version = Azure::Loganalytics::Datacollectorapi::VERSION
9
+ spec.authors = ["Yoichi Kawasaki"]
10
+ spec.email = ["yoichi.kawasaki@outlook.com"]
11
+ spec.summary = %q{Azure Log Analytics Data Collector API Ruby Client}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/yokawasa/azure-log-analytics-data-collector"
14
+ spec.license = "MIT"
15
+
16
+ # Tests
17
+ spec.files = `git ls-files`.split("\n")
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rest-client"
23
+ spec.add_development_dependency "bundler", "~> 1.13"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "azure/loganalytics/datacollectorapi/client"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,70 @@
1
+ require "azure/loganalytics/datacollectorapi/version"
2
+
3
+ module Azure
4
+ module Loganalytics
5
+ module Datacollectorapi
6
+
7
+ API_VERSION = '2016-04-01'.freeze
8
+
9
+ class Client
10
+
11
+ def initialize (customer_id, shared_key)
12
+ require 'rest-client'
13
+ require 'json'
14
+ require 'openssl'
15
+ require 'base64'
16
+ require 'time'
17
+
18
+ @customer_id = customer_id
19
+ @shared_key = shared_key
20
+ end
21
+
22
+ def post_data(log_type, json_records, record_timestamp ='')
23
+ raise ConfigError, 'no log_type' if log_type.empty?
24
+ raise ConfigError, 'log_type must be only alpha characters' if not is_alpha(log_type)
25
+ raise ConfigError, 'no json_records' if json_records.empty?
26
+ body = json_records.to_json
27
+ uri = sprintf("https://%s.ods.opinsights.azure.com/api/logs?api-version=%s",
28
+ @customer_id, API_VERSION)
29
+ date = rfc1123date()
30
+ sig = signature(date, body.bytesize)
31
+
32
+ headers = {
33
+ 'Content-Type' => 'application/json',
34
+ 'Authorization' => sig,
35
+ 'Log-Type' => log_type,
36
+ 'x-ms-date' => date,
37
+ 'time-generated-field' => record_timestamp
38
+ }
39
+
40
+ res = RestClient.post( uri, body, headers)
41
+ res
42
+ end
43
+
44
+ private
45
+
46
+ def is_alpha(s)
47
+ return (s.match(/^[[:alpha:]]+$/)) ? true : false
48
+ end
49
+
50
+ def rfc1123date()
51
+ t = Time.now
52
+ t.httpdate()
53
+ end
54
+
55
+ def signature(date, content_length)
56
+ sigs = sprintf("POST\n%d\napplication/json\nx-ms-date:%s\n/api/logs",
57
+ content_length, date)
58
+ utf8_sigs = sigs.encode('utf-8')
59
+ decoded_shared_key = Base64.decode64(@shared_key)
60
+ hmac_sha256_sigs = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), decoded_shared_key, utf8_sigs)
61
+ encoded_hash = Base64.encode64(hmac_sha256_sigs)
62
+ authorization = sprintf("SharedKey %s:%s", @customer_id,encoded_hash)
63
+ authorization
64
+ end
65
+
66
+ end # end of class
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,11 @@
1
+ module Azure
2
+ module Loganalytics
3
+ module Datacollectorapi
4
+
5
+ def is_success(res)
6
+ return (res.code == 202) ? true : false
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Azure
2
+ module Loganalytics
3
+ module Datacollectorapi
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ describe Azure::Loganalytics::Datacollectorapi::Client do
4
+ it "has a version number" do
5
+ expect(Azure::Loganalytics::Datacollectorapi::VERSION).not_to be nil
6
+ end
7
+
8
+ #it "does something useful" do
9
+ # expect(false).to eq(true)
10
+ #end
11
+
12
+ it "posting data to datacollector api" do
13
+ customer_id = '<Customer ID aka WorkspaceID String>'
14
+ shared_key = '<Primary Key String>'
15
+ log_type = "ApacheAccessLog"
16
+
17
+ json_records = []
18
+ record1= {
19
+ :Log_ID => "5cdad72f-c848-4df0-8aaa-ffe033e75d57",
20
+ :date => "2016-12-03 09:44:32 JST",
21
+ :processing_time => "372",
22
+ :remote => "101.202.74.59",
23
+ :user => "-",
24
+ :method => "GET / HTTP/1.1",
25
+ :status => "304",
26
+ :size => "-",
27
+ :referer => "-",
28
+ :agent => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:27.0) Gecko/20100101 Firefox/27.0"
29
+ }
30
+ record2 ={
31
+ :Log_ID => "7260iswx-8034-4cc3-uirtx-f068dd4cd659",
32
+ :date => "2016-12-03 09:45:14 JST",
33
+ :processing_time => "105",
34
+ :remote => "201.78.74.59",
35
+ :user => "-",
36
+ :method => "GET /manager/html HTTP/1.1",
37
+ :status =>"200",
38
+ :size => "-",
39
+ :referer => "-",
40
+ :agent => "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0"
41
+ }
42
+ json_records.push(record1)
43
+ json_records.push(record2)
44
+
45
+ client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
46
+ res = client.post_data(log_type, json_records)
47
+ expect(res.code).to eq(202)
48
+ end
49
+
50
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "azure/loganalytics/datacollectorapi/client"
3
+ require "azure/loganalytics/datacollectorapi/helper"
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: azure-loganalytics-datacollector-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoichi Kawasaki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Azure Log Analytics Data Collector API Ruby Client
70
+ email:
71
+ - yoichi.kawasaki@outlook.com
72
+ executables:
73
+ - console
74
+ - setup
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - azure-loganalytics-datacollector-api.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/azure/loganalytics/datacollectorapi/client.rb
88
+ - lib/azure/loganalytics/datacollectorapi/helper.rb
89
+ - lib/azure/loganalytics/datacollectorapi/version.rb
90
+ - spec/azure/loganalytics/datacollectorapi/client_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/yokawasa/azure-log-analytics-data-collector
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Azure Log Analytics Data Collector API Ruby Client
116
+ test_files:
117
+ - spec/azure/loganalytics/datacollectorapi/client_spec.rb
118
+ - spec/spec_helper.rb