# InSpec - checking files/directories when testing remote hosts

**URL:** https://discourse.chef.io/t/inspec-checking-files-directories-when-testing-remote-hosts/14422
**Category:** Chef InSpec
**Created:** [January 29, 2019, 2:55pm UTC](https://discourse.chef.io/t/inspec-checking-files-directories-when-testing-remote-hosts/14422 "2019-01-29T14:55:50Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![netfab](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@netfab](https://discourse.chef.io/u/netfab)
#### Post date: [January 29, 2019, 2:55pm UTC](https://discourse.chef.io/t/inspec-checking-files-directories-when-testing-remote-hosts/14422/1 "2019-01-29T14:55:50Z")

</div>

Hey there!  
I have an InSpec control, which is executed against remote hosts. What I want to achieve is to check the modification time of all the files in a specific location. Works fine on localhost. However, when running against a remote system, the log file arrays are evaluated locally, which means InSpec looks into the directories on the localhost, fills the arrays and then tries to access all the files on the remote hosts. Some of them exist, but most of them don't so I get a bunch of errors. Is this intended? And how can I achieve that the arrays are filled with actual values on the remote host?  
I already tried to put the first to lines into the control, directly before the forEach loop, did not help.  
Any ideas? Help would be much appreciated!

nginx\_log\_files = Dir["/var/log/nginx/_._"]  
mysql\_log\_files = Dir["/var/log/mysql/_._"]

control:

control 'testcase-19-os' do  
impact 1.0  
title 'Log files'  
desc 'Check whether log file modification time is \<30 days'  
nginx\_log\_files.each do |path|  
describe file(path) do  
its('mtime') { should \>= Time.now.to\_i - 30_24_60_60 }  
end  
end  
mysql\_log\_files.each do |path|  
describe file(path) do  
its('mtime') { should \>= Time.now.to\_i - 30_24_60_60 }  
end  
end  
end

output:  
× testcase-19-os: Log files (13 failed)  
× File /var/log/nginx/error.log.1 mtime should \>= 1546180951  
undefined method `>=' for nil:NilClass × File /var/log/nginx/access.log mtime should >= 1546180951 undefined method`\>=' for nil:NilClass  
× File /var/log/nginx/error.log mtime should \>= 1546180951  
undefined method `>=' for nil:NilClass × File /var/log/nginx/access.log.2.gz mtime should >= 1546180951 undefined method`\>=' for nil:NilClass  
× File /var/log/nginx/access.log.1 mtime should \>= 1546180951  
undefined method `>=' for nil:NilClass ✔ File /var/log/mysql/error.log.5.gz mtime should >= 1546180951 × File /var/log/mysql/mysql.log.1.gz mtime should >= 1546180952 undefined method`\>=' for nil:NilClass  
✔ File /var/log/mysql/error.log.1.gz mtime should \>= 1546180952  
× File /var/log/mysql/mysql.log mtime should \>= 1546180952  
undefined method `>=' for nil:NilClass × File /var/log/mysql/mysql-slow.log.2.gz mtime should >= 1546180952 undefined method`\>=' for nil:NilClass  
× File /var/log/mysql/mysql-slow.log.3.gz mtime should \>= 1546180952  
undefined method `>=' for nil:NilClass × File /var/log/mysql/mysql.log.2.gz mtime should >= 1546180952 undefined method`\>=' for nil:NilClass  
× File /var/log/mysql/mysql.log.3.gz mtime should \>= 1546180952  
undefined method `>=' for nil:NilClass ✔ File /var/log/mysql/error.log.2.gz mtime should >= 1546180952 ✔ File /var/log/mysql/error.log.3.gz mtime should >= 1546180953 × File /var/log/mysql/mysql-slow.log.1.gz mtime should >= 1546180953 undefined method`\>=' for nil:NilClass  
✔ File /var/log/mysql/error.log.7.gz mtime should \>= 1546180953  
✔ File /var/log/mysql/error.log mtime should \>= 1546180953  
× File /var/log/mysql/mysql-slow.log mtime should \>= 1546180953  
undefined method `\>=' for nil:NilClass  
✔ File /var/log/mysql/error.log.4.gz mtime should \>= 1546180953

---

<div class="post-metadata">

### Author: ![netfab](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@netfab](https://discourse.chef.io/u/netfab)
#### Post date: [February 2, 2019, 8:34pm UTC](https://discourse.chef.io/t/inspec-checking-files-directories-when-testing-remote-hosts/14422/2 "2019-02-02T20:34:19Z")

</div>

Okay, turns out this is not really InSpec related but just the way the 'Dir' resource within Ruby works.  
I found a workaround, not sure if it's the smartest one but I'll share it anyway:

Instead of using the Dir function I invoked a command on the remote host to list all the files in the desired directory and then split it so that I could fill my arrays with it:

nginx\_log\_files = command('ls /var/log/nginx/_._').stdout.split("\n")  
mysql\_log\_files = command('ls /var/log/mysql/_._').stdout.split("\n")
