# Read chef-client log

**URL:** <https://discourse.chef.io/t/read-chef-client-log/10340>\
**Category:** Chef Infra (archive)\
**Created:** [February 1, 2017, 11:44pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340 "2017-02-01T23:44:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chic\_cat](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@chic\_cat](https://discourse.chef.io/u/chic_cat)\
**Post date:** [February 1, 2017, 11:44pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340/1 "2017-02-01T23:44:33Z")

</div>

Hi guys, I’m trying to send clien run log via email,

Chef.event\_handler do  
on :run\_failed do  
Email.new(Chef.run\_context.node.name, true, def\_emails).runtime\_send;  
end  
end

Inside Email.runtime\_send I just read log file /var/chef/run.log, but it appears ruby pulls before chef-client stops working  
Chef reads this:  
[2017-02-01T23:47:08+00:00] ERROR: Running exception handlers  
[2017-02-01T23:47:08+00:00] ERROR: Exception handlers complete

Log file much bigger:  
[2017-02-01T23:47:08+00:00] ERROR: Running exception handlers  
[2017-02-01T23:47:08+00:00] ERROR: Exception handlers complete  
[2017-02-01T23:47:18+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out  
[2017-02-01T23:47:18+00:00] FATAL: Please provide the contents of the stacktrace.out file if you file a bug report  
[2017-02-01T23:47:18+00:00] ERROR: server\_type is not defined  
[2017-02-01T23:47:18+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)

Any chance to fix this?

Thanks

---

<div class="post-metadata">

**Author:** ![joerg.herzinger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joerg.herzinger/32/1514_2.png) [@joerg.herzinger](https://discourse.chef.io/u/joerg.herzinger)\
**Post date:** [February 2, 2017, 2:18pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340/2 "2017-02-02T14:18:15Z")

</div>

Well, the relevant error is:

> [@chic\_cat](#):
>
> [2017-02-01T23:47:18+00:00] ERROR: server\_type is not defined

So without the actual code of your Email Class/Module this will be quite impossible to solve.

---

<div class="post-metadata">

**Author:** ![chic\_cat](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@chic\_cat](https://discourse.chef.io/u/chic_cat)\
**Post date:** [February 2, 2017, 4:58pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340/3 "2017-02-02T16:58:28Z")

</div>

server\_type - it’s raise I send in recipe when some conditions happen, so the mail idea send the whole log, here’s the code

recipe/default.rb

# exceptions handler

Chef.event\_handler do  
on :run\_failed do  
sleep(10);  
Email.new(Chef.run\_context.node.name, true, def\_emails).runtime\_send;  
end  
on :run\_completed do  
Email.new(Chef.run\_context.node.name, false, def\_emails).runtime\_send;  
end  
end

---

<div class="post-metadata">

**Author:** ![chic\_cat](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@chic\_cat](https://discourse.chef.io/u/chic_cat)\
**Post date:** [February 2, 2017, 5:03pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340/4 "2017-02-02T17:03:30Z")

</div>

require ‘net/smtp’

class Email \< ::Chef::EventDispatch::Base  
def initialize(node\_name=“unk”,exec\_status=true, def\_emails={“from” =\> “[emailmy.com](http://emailmy.com)”, “to” =\> “[emailmy.com](http://emailmy.com)”})  
node\_name=node\_name  
exec\_status=exec\_status  
def\_emails=def\_emails  
end

def runtime\_send  
node\_name=node\_name  
def\_emails=def\_emails  
log\_file=’/var/chef/run.log’

```
if(exec_status == true)
    subject = 'Chef job failed'
    log_content = File.read(log_file)
else
    subject = 'Chef job completed'
    log_content='';
end
message = "From: #{def_emails['from']}>\n"
message << "To: #{def_emails['to']}\n"
message << "Subject: #{subject}\n"
message << "Date: #{Time.now.rfc2822}\n\n"
message << "Node: #{node_name} \n\n #{log_content} \n"
Net::SMTP.start('mailhost', 25) do |smtp|
  smtp.send_message message, "#{def_emails['from']}", "#{def_emails['to']}"
end

```

end  
end

---

<div class="post-metadata">

**Author:** ![joerg.herzinger](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/joerg.herzinger/32/1514_2.png) [@joerg.herzinger](https://discourse.chef.io/u/joerg.herzinger)\
**Post date:** [February 2, 2017, 8:43pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340/5 "2017-02-02T20:43:50Z")

</div>

First off: Please use proper formatting in your messages.

> [@chic\_cat](#):
>
> recipe/default.rb

Is that handler code really what you have in your recipe? If so, than this is wrong. The handler code should be shipped using a cookbook\_file, template, ruby gem or any similar way. The recipe should then contain a block pointing to that handler code, see the chef\_hander cookbook [1].

[1] [GitHub - chef-boneyard/chef\_handler: DEPRECATED: Development repository for Chef Cookbook chef\_handler](https://github.com/chef-cookbooks/chef_handler)

---

<div class="post-metadata">

**Author:** ![chic\_cat](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@chic\_cat](https://discourse.chef.io/u/chic_cat)\
**Post date:** [February 2, 2017, 8:48pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340/6 "2017-02-02T20:48:25Z")

</div>

Hmm, I used this manual to create it ( [https://docs.chef.io/handlers.html](https://docs.chef.io/handlers.html)) ,  
Actually it’s not possible to handle fatal errors during that cookbook, tried to do that - it would fail to send any emails.

And my code shipped as a cookbook, then I run on clients

---

<div class="post-metadata">

**Author:** ![chic\_cat](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@chic\_cat](https://discourse.chef.io/u/chic_cat)\
**Post date:** [February 2, 2017, 9:13pm UTC](https://discourse.chef.io/t/read-chef-client-log/10340/7 "2017-02-02T21:13:38Z")

</div>

Taking my words back, it works. Thanks!
