Class: Fluent::Pi

Inherits:
BufferedOutput
  • Object
show all
Defined in:
lib/fluent/plugin/out_pi.rb

Constant Summary collapse

DEFAULT_FORMAT_TYPE =
'json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePi



52
53
54
55
# File 'lib/fluent/plugin/out_pi.rb', line 52

def initialize
  super
  @delayed = false
end

Instance Attribute Details

#delayedObject

Returns the value of attribute delayed.



50
51
52
# File 'lib/fluent/plugin/out_pi.rb', line 50

def delayed
  @delayed
end

Instance Method Details

#configure(conf) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fluent/plugin/out_pi.rb', line 57

def configure(conf)
  if conf['output_type'] && !conf['format']
    conf['format'] = conf['output_type']
  end
  compat_parameters_convert(conf, :inject, :formatter)

  super

  @formatter = formatter_create(conf: conf.elements('format').first, default_type: DEFAULT_FORMAT_TYPE)

  #set piwebapi URI information

  @piwebapiserver = conf['piwebapiserver']
  
  (conf.key?('tagpath')) ? @tagpath = conf['tagpath'] : @tagpath = nil
  (conf.key?('attpath')) ? @attpath = conf['attpath'] : @attpath = nil
   
  if(@tagpath == nil && @attpath == nil)
    $log.write("Neither a tag nor attribute is specified.  Unable to log to PI.\n")
    return
  end

  if(@tagpath != nil && @attpath != nil)
    $log.write("Both a tag and attribute is specified.  Using the attribute.\n")
  end
  
  (conf.key?('valuetag')) ? @valuetag = conf['valuetag'] : @valuetag = 'Value'
  (conf.key?('username')) ? @username = conf['username'] : @username = nil    
  (conf.key?('pwd')) ? @pwd = conf['pwd'] : @pwd = nil
  (conf.key?('anon')) ? @anon = conf['anon'] : @anon = false

  (conf.key?('webid')) ? @webid = conf['webid'] : @webid = nil
  
  if((@username == nil) && !@anon)
    $log.write("Please specify a username, or specify that anonymous will be used.\n")
    return
  end
end

#format(tag, time, record) ⇒ Object



145
146
147
148
# File 'lib/fluent/plugin/out_pi.rb', line 145

def format(tag, time, record)
    value = record[@valuetag].to_s
    "{\"Timestamp\":\"#{Time.at(time).localtime}\",\"Value\":\"#{value}\"},"
end

#prefer_buffered_processingObject



42
43
44
# File 'lib/fluent/plugin/out_pi.rb', line 42

def prefer_buffered_processing
  false
end

#prefer_delayed_commitObject



46
47
48
# File 'lib/fluent/plugin/out_pi.rb', line 46

def prefer_delayed_commit
  @delayed
end

#startObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/fluent/plugin/out_pi.rb', line 95

def start
  super
  # need to populate webid if not provided


  if(@webid != nil)
    return
  end

  client = nil  
 #PI Web API search doesn't work from basic or anonymous.  Kerberos from Ruby isn't working.  I think we have to use the absolute path.  With Kerberos we could looking based on search and use top result

  if(@attpath == nil)
    url = "https://#{@piwebapiserver}/piwebapi/points/?path=#{@tagpath}"
    if(@anon)
      client = RestClient::Resource.new(url, :verify_ssl => OpenSSL::SSL::VERIFY_NONE )
    else
      client = RestClient::Resource.new(url, :verify_ssl => OpenSSL::SSL::VERIFY_NONE, :user => @username, :password =>@pwd)
    end
  else 
    url = "https://#{@piwebapiserver}/piwebapi/attributes/?path=#{@attpath}"
    if(@anon)
      client = RestClient::Resource.new(url, :verify_ssl => OpenSSL::SSL::VERIFY_NONE )
    else
      client = RestClient::Resource.new(url, :verify_ssl => OpenSSL::SSL::VERIFY_NONE, :user => @username, :password =>@pwd)
    end
  end
                              
  response = client.get
  @webid = (JSON.parse(response.body))['WebId']
  if(@webid == nil)
    $log.write("WedID is lost.  Unable to log to PI\n")
    return
  end

  @writeURL = "https://#{@piwebapiserver}/piwebapi/streams/#{@webid}/recorded"
  if(@anon)
    @writeclient = RestClient::Resource.new(@writeURL, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
  else
    @writeclient = RestClient::Resource.new(@writeURL, :verify_ssl => OpenSSL::SSL::VERIFY_NONE,:user => @username, :password =>@pwd)
  end

end

#write(chunk) ⇒ Object



137
138
139
140
141
142
143
# File 'lib/fluent/plugin/out_pi.rb', line 137

def write(chunk)
    data= chunk.read
    jsonObj = "[" + data.chop + "]" # removes trailing ',' and makes a valid json object            


    response = @writeclient.post jsonObj, :content_type => 'application/json'                   
    $log.write("PI successful writes\n")
end