LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create a script that mounts a share, checks a file if it has not been modified in 2 or 1 days, and then unmounts it? (https://www.linuxquestions.org/questions/linux-newbie-8/create-a-script-that-mounts-a-share-checks-a-file-if-it-has-not-been-modified-in-2-or-1-days-and-then-unmounts-it-4175712180/)

riahc3 05-16-2022 05:53 AM

Create a script that mounts a share, checks a file if it has not been modified in 2 or 1 days, and then unmounts it?
 
Hello

In a monitoring system I have, I had a plugin that remotely checked a SMB share and if the file hasnt been modified in 1 day, it should return a warning. if it is 2 days or more, it should return a critical.

Returning the warning and the critical isnt the issue, its the entire logic of the script itself.

I mean, I dont think it should be too difficult but Im not used to shell scripting. Also, the downfall exists, of having to check if the mount is correct, etc.

Could someone tell me the basics of the shell script?

Thank you.

boughtonp 05-16-2022 08:20 AM


 
Mounting the filesystem is done with mount - if it succeeds, the mount exit status will be zero, otherwise it wont.

Unmounting is done with umount (note: not unmount) - you just run that command regardless and use --quiet option to tell it not to complain if it wasn't mounted.

Checking whether a file has been modified is something find can do - you can use the -mtime option with 24 and 48, and then either check the find exit status, or use the -exec action to output the warning, or perhaps instead use -printf format then do the comparison with something else.

For checking exit status, you can usually just do if command, but sometimes reading the value of $?, and sometimes chaining with "command1 && command2 && command3". (See also BashGuide/TestsAndConditionals.)

Whatever you do, using Shellcheck is highly recommended.


rtmistler 05-16-2022 08:28 AM

Sounds like all you need to do is run that plug-in and then test the result.

Everything you can type into the command line, you can put into a shell script.

For when you need to make decisions or iterate, she'll scripts support if-elseif-else and loops.

So suggest you write down your steps in order and then assess if you need more than <step>-<step>-<step> and instead need a loop or if-test.

I think you'll only need either an if-test or a case statement.

We can help you with the logic to write your tests and/or loops.

Finally this seems like it will run periodically, so a cron job is an excellent way to run a script on a periodic basis.

jmgibson1981 05-16-2022 09:54 AM

I can't tell if the SMB share is linux or other? If it's linux can't you avoid the mount process altogether and just run a find command over ssh? Then react on the results? This also wouldn't require it to be run as root / or with an fstab entry with the user field.

Something generally like this?

Code:

#!/bin/sh
TARGET=$(ssh user@target "find /path/to/folder/ -mtime +1 -type f")

[ -z "$TARGET" ] && (react if not found)


Rickkkk 05-16-2022 02:18 PM

Quote:

Originally Posted by jmgibson1981 (Post 6353804)
I can't tell if the SMB share is linux or other? If it's linux can't you avoid the mount process altogether and just run a find command over ssh? Then react on the results? This also wouldn't require it to be run as root / or with an fstab entry with the user field.

Something generally like this?

Code:

#!/bin/sh
TARGET=$(ssh user@target "find /path/to/folder/ -mtime +1 -type f")

[ -z "$TARGET" ] && (react if not found)


.... good idea. You can also SSH into a Windows host.

Rick

riahc3 05-17-2022 08:31 AM

My logic code would be this:

inputvariable warningdays
inputvariable criticaldays
inputvariable smbpath
mount smbpath to final folder /mnt/folder
if mount is unsuccessful
return unknown
if mount is successful
if /mnt/folder/file does not exist
return unknown
if /mnt/folder/file does exist
if /mnt/folder/file has been written to less than warningdays
return ok
if /mnt/folder/file has been written to more than warningdays and less than criticaldays
return warning
if /mnt/folder/file has been written to more than criticaldays
return critical
if previousoperations are done
unmount
if unmount is unsuccessful
return unknown
finish

How would I write that in a shell script?

boughtonp 05-17-2022 09:01 AM

Quote:

Originally Posted by riahc3 (Post 6354121)
How would I write that in a shell script?

Read, understand and apply what you were told in post #2. (You may also find Bash Conditional Expressions useful.)

If there's any part of what I've written which isn't sufficiently clear, say so and someone can explain further.

If you attempt to write a Bash script and post that here (after running it through Shellcheck) you'll get advice on any issues, but your post reads a bit like you want someone to just do it for you (if that is what you want, hire a consultant), and LQ is about helping people to help themselves.


riahc3 05-17-2022 09:15 AM

Quote:

Originally Posted by boughtonp (Post 6354128)
your post reads a bit like you want someone to just do it for you

This script at most Id say takes up 20 lines

Reading thru all the documentation would at least take me 30 minutes.

Someone here can problably write up the script in 30 minutes.

Lets say I start writing the script: At least 2 hours since I dont know much (if at all) about shell scripting.

So, in my mind, I see as logical to bill the end client 30 minutes for a POS script vs 2 hours and telling him they have to pay for me (or others) reading the documentation.

You have a close friend that is a mechanic. Are you gonna ask him a question that takes 5-10 minutes to REPLY or are you gonna call up the garage, setup up a appointment, bring your car in, have the mechanic look at it for 1 hour and then tell you the same thing your close friend would? Its counterproductive and time wasting

Thats my POV :)

jmgibson1981 05-17-2022 09:37 AM

You should bill the client the time required. If you can't do it yet offered / claimed you could then you screwed up and that is on you. If they don't want to pay it then they can do it. Simple concept. Don't work for free (unless you lied to them and said in which case again... on you).

computersavvy 05-17-2022 10:56 AM

Quote:

Originally Posted by riahc3 (Post 6354133)
This script at most Id say takes up 20 lines

Reading thru all the documentation would at least take me 30 minutes.

Someone here can problably write up the script in 30 minutes.

Lets say I start writing the script: At least 2 hours since I dont know much (if at all) about shell scripting.

So, in my mind, I see as logical to bill the end client 30 minutes for a POS script vs 2 hours and telling him they have to pay for me (or others) reading the documentation.

You have a close friend that is a mechanic. Are you gonna ask him a question that takes 5-10 minutes to REPLY or are you gonna call up the garage, setup up a appointment, bring your car in, have the mechanic look at it for 1 hour and then tell you the same thing your close friend would? Its counterproductive and time wasting

Thats my POV :)


I don't care about your POV.

If the mechanic is on the job and being paid I would expect to pay his employer for the time involved.

If I want to know how to do things for myself then the time spent in learning how to do that is time well spent.

Your posted POV is that our time is owed to you, but since we are all volunteers we do not feel the same.

You make an effort, ask about where you are having problems, we can point out improvements. You make no effort we provide no assistance.

Oh, BTW --- I would volunteer to write the 'minor' script for you at $75 per hour, with a 2 hour minimum paid in advance. That would be my 'consulting fee'


Ahh, you are discounting the time involved in prep (reading the documentation so one knows what is needed / expected) as not worth being paid for. What a concept! Giving away your time for free!!

riahc3 05-18-2022 02:23 AM

Quote:

Originally Posted by computersavvy (Post 6354176)
I don't care about your POV.

Well, you did reply to the post. Hypocrisy at its best.

riahc3 05-18-2022 02:25 AM

Quote:

Originally Posted by jmgibson1981 (Post 6354146)
You should bill the client the time required.

Why?

Over half of the things any IT person does is Google where more than 25% of the time, a solution already exists

pan64 05-18-2022 03:41 AM

Quote:

Originally Posted by riahc3 (Post 6354368)
Why?

Over half of the things any IT person does is Google where more than 25% of the time, a solution already exists

In that case you need to pay for that activity. Looking for a solution for you.

rtmistler 05-18-2022 01:19 PM

I'd hate for the discussion to degrade into arguments about time, convenience, or who should do the task.

As noted by another member, LQ works more where we help you to accomplish your goal, using your own technical efforts. Because volunteers on this site are not customer service but here to share and discuss technical challenges related to Linux.

That said, I've certainly seen examples where a member has chosen to help freely and they post a solution as well as help you to refine it to work. No guarantees this will happen.

Back to my first sentence about this thread not degrading, let's all please not argue about who would do it, how one would bill for it, and etc.

If the OP only wishes an option for someone to do the effort for them, then there's nothing to add unless a member decides to do so freely, or contacts the OP offline to discuss a possible paid opportunity.

Meanwhile riahc3,

You should not bump this thread to persist with checking to see if someone new sees it and considers providing a solution. And if your interest is to have someone do effort for you, for hire, you can ask people to PM you, but not post a contact email or discuss terms and details within a technical thread. This is not a forum which supports advertising work for hire.

riahc3 05-19-2022 04:28 AM

Im already writing the script as best I can. Ill post it for feedback (or is feedback also "paid"???)


All times are GMT -5. The time now is 04:13 AM.